我有自定义帖子类型,如
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 );
和像Metabox一样
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
和Post
上显示,但不会显示在自定义帖子类型ProjectsCPT
上。您能告诉我我的错误吗? (如果我删除if语句
if (in_array($post_type, $types)) {}
但是这会将元数据添加到所有帖子和页面,这不是我需要做的事情
答案 0 :(得分:0)
是否可以在'rewrite' => array('slug' => 'ProjectsCPT'),
register_post_type
中添加$args
以下示例:
$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,
'rewrite' => array('slug' => 'ProjectsCPT'),
'capability_type' => 'post',
);
添加元数据
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',
$types,
'normal',
'high'
);
}
}