wordpress 如何将自定义字段添加到自定义帖子类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11740907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add custom field to custom post type?
提问by Cynthia
Good morning.
早上好。
I have created a custom post type called 'Products'. I want to create a custom field (is metabox the correct term?) where my client can tick a box to determine whether a given post within this CPT is a featured post.
我创建了一个名为“产品”的自定义帖子类型。我想创建一个自定义字段(metabox 是正确的术语吗?)我的客户可以在其中勾选一个框来确定此 CPT 中的给定帖子是否是精选帖子。
Here is the code in my functions.php to create the 'Products' CPT:
这是我的functions.php 中用于创建“产品”CPT 的代码:
function products_custom_init() {
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'products'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_items' => __('Search Products'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'query_var' => true,
'rewrite' => array('slug','pages'),
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail','excerpt',)
);
register_post_type( 'products' , $args );
}
add_action( 'init', 'products_custom_init' );
So how do I add the 'featured' metabox / custom field to onlyProducts posts?
那么如何将“特色”元框/自定义字段仅添加到产品帖子中?
Many thanks,
非常感谢,
Cynthia
辛西娅
采纳答案by Muhammad Yasin
You can use this plugin
你可以使用这个插件
http://wordpress.org/extend/plugins/types/
http://wordpress.org/extend/plugins/types/
or this tutorial may helpful for you.
或者本教程可能对您有所帮助。
回答by janw
As Muhammad Yasin said there are plugins I'd recommend:
http://wordpress.org/extend/plugins/more-fields/
正如穆罕默德·亚辛所说,我推荐一些插件:http:
//wordpress.org/extend/plugins/more-fields/
if you want to do it yourself in code look at: add_meta_box
如果你想在代码中自己做,请查看: add_meta_box
<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>
You can register boxes per post type.
您可以为每种帖子类型注册框。
回答by Frits
If you want to create a custom meta box inside a custom post type you will need to use 3 functions.
如果要在自定义帖子类型中创建自定义元框,则需要使用 3 个函数。
A function to create a custom "meta boxes" block/screen on your post edit screen:
add_meta_boxes_{$post_type}
A function to add a input field to change/show your custom meta
And finally a function to save your meta along with the rest of the post when you click save on the post edit screen:
save_post_{$post->post_type}
在您的帖子编辑屏幕上创建自定义“元框”块/屏幕的功能:
add_meta_boxes_{$post_type}
添加输入字段以更改/显示您的自定义元数据的功能
最后还有一个功能,当您在帖子编辑屏幕上单击“保存”时,可以将您的元数据与帖子的其余部分一起保存:
save_post_{$post->post_type}
In your case, a custom checkbox would look like this:
在您的情况下,自定义复选框如下所示:
add_action( 'add_meta_boxes_products', 'meta_box_for_products' );
function meta_box_for_products( $post ){
add_meta_box( 'my_meta_box_custom_id', __( 'Additional info', 'textdomain' ), 'my_custom_meta_box_html_output', 'products', 'normal', 'low' );
}
function my_custom_meta_box_html_output( $post ) {
wp_nonce_field( basename( __FILE__ ), 'my_custom_meta_box_nonce' ); //used later for security
echo '<p><input type="checkbox" name="is_this_featured" value="checked" '.get_post_meta($post->ID, 'team_member_title', true).'/><label for="is_this_featured">'.__('Featured Product?', 'textdomain').'</label></p>';
}
add_action( 'save_post_team_member', 'team_member_save_meta_boxes_data', 10, 2 );
function team_member_save_meta_boxes_data( $post_id ){
// check for nonce to top xss
if ( !isset( $_POST['my_custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['my_custom_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// check for correct user capabilities - stop internal xss from customers
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// update fields
if ( isset( $_REQUEST['is_this_featured'] ) ) {
update_post_meta( $post_id, 'is_this_featured', sanitize_text_field( $_POST['is_this_featured'] ) );
}
}