我有一个自定义的帖子类型,如
function cpt_Projects() {
$labels = array(
\'name\' => \'Projects\',
\'singular_name\' => \'Project\',
\'menu_name\' => \'Projects\',
\'name_admin_bar\' => \'Projects\',
\'parent_item_colon\' => \'Parent Projects:\',
\'all_items\' => \'All Projects\',
\'view_item\' => \'View Project\',
\'add_new_item\' => \'Add New Project\',
\'add_new\' => \'Add New Project\',
\'new_item\' => \'New Projects\',
\'edit_item\' => \'Edit Project Item\',
\'update_item\' => \'Update Project Item\',
\'search_items\' => \'Search Project Item\',
\'not_found\' => \'Project Not found\',
\'not_found_in_trash\' => \'Project Not found in Trash\',
);
$args = array(
\'label\' => \'ProjectsCPT\',
\'description\' => \'This Post Type Adds Eyeglasses to Website\',
\'labels\' => $labels,
\'supports\' => array( \'title\', \'thumbnail\', \'editor\'),
\'taxonomies\' => array( \'ProjectsTax\' ),
\'register_meta_box_cb\' => \'add_details_metabox\',
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
);
register_post_type( \'ProjectsCPT\', $args );
}
add_action( \'init\', \'cpt_Projects\', 0 );
还有一个代谢箱
function add_details_metabox($post_type) {
$types = array(\'post\', \'page\', \'ProjectsCPT\');
if (in_array($post_type, $types)) {
add_meta_box(
\'details-metabox\',
\'Project Details\',
\'detail_meta_callback\',
$post_type,
\'normal\',
\'high\'
);
}
}
运行代码后,Metabox将显示在所有
Page
s和
Post
s,但不在自定义帖子类型上
ProjectsCPT
你能告诉我我做错了什么吗?(如果删除if语句,效果很好
if (in_array($post_type, $types)) {}
但这将metabox添加到所有帖子和页面,这不是我需要做的
SO网友:Sumit
请注意add_meta_boxes
和register_meta_box_cb
.
当您使用注册元框时add_meta_boxes
, 以下是WordPress的调用方式do_action()
/**
* Fires after all built-in meta boxes have been added.
*
* @since 3.0.0
*
* @param string $post_type Post type.
* @param WP_Post $post Post object.
*/
do_action( \'add_meta_boxes\', $post_type, $post );
第一个参数是post类型,第二个参数是post对象。
当使用注册回调时register_meta_box_cb
然后在register_post_type()
功能WordPress添加操作。
if ( $args->register_meta_box_cb )
add_action( \'add_meta_boxes_\' . $post_type, $args->register_meta_box_cb, 10, 1 );
那么
/**
* Fires after all built-in meta boxes have been added, contextually for the given post type.
*
* The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
*
* @since 3.0.0
*
* @param WP_Post $post Post object.
*/
do_action( \'add_meta_boxes_\' . $post_type, $post );
所以第一个也是唯一的论点是
$post
post对象,您正在将其用作post类型。
在比较之前,您必须先获取帖子类型。
示例:-
function add_details_metabox($post) {
$types = array(\'post\', \'page\', \'projectscpt\');
$post_type = get_post_type($post);
if (in_array($post_type, $types)) {
add_meta_box(
\'details-metabox\',
\'Project Details\',
\'detail_meta_callback\',
$post_type,
\'normal\',
\'high\'
);
}
}
NOTE #1 正如@vancoder所回答的,无论您在post类型中传递什么情况。它被转换为小写。WordPress建议注册没有空格和大写字母的帖子类型。您必须使用
projectscpt
而不是大写字母。
NOTE #2 使用时register_meta_box_cb
它将回调函数挂接在add_meta_boxes_{post_type}
行动此元框不会出现在任何其他帖子类型上。因此,您不需要在回调函数中检查post类型。如果您在其他帖子类型上也得到了元框,那么请检查代码,也许您犯了一些错误。