我已经为我的项目创建了自定义帖子类型,我想添加带有页面属性的自定义帖子类型。我已经用下面的代码做到了这一点:
function register_pt_boutique()
{
register_post_type('boutique', array(
'label' => 'Boutiques',
'description' => 'This post type represents the featured items on the homepage.',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
// 'rewrite' => array('slug' => 'featured', 'hierarchical' => true, 'with_front' => false),
'query_var' => true,
'has_archive' => true,
'menu_position' => '7',
'supports' => array('title', 'page-attributes','thumbnail'),
// 'taxonomies' => array('page-attributes'),
'labels' => array(
'name' => 'Boutiques',
'singular_name' => 'Boutique',
'menu_name' => 'Boutiques',
'add_new' => 'Add Boutique',
'add_new_item' => 'Add New Boutique',
'edit' => 'Edit',
'edit_item' => 'Edit Boutique',
'new_item' => 'New Boutique',
'view' => 'View Boutique',
'view_item' => 'View Boutique',
'search_items' => 'Search Boutique',
'not_found' => 'No Boutique',
'not_found_in_trash' => 'No Boutique Found in Trash',
'parent' => 'Parent Boutique',
)
));
}
使用此代码页属性选项时,管理员端有两个字段("parent“和"order"),但没有"template”字段选项。我想要具有“模板”字段的页面属性。
发布于 2020-05-14 00:59:01
正如您在页面模板文件中给出的模板名称一样:
Template Name: <template name>您可以将自定义帖子类型指定为要显示的模板帖子类型:
Template Post Type: <custom_post_type_1>, <custom_post_type_2>, <custom_post_type_3>例如:我想要将页面属性添加到我的自定义帖子类型之一的项目组合中:
在我的页面模板中,我添加了:
Template Name: Portfolio Design
Template Post Type: portfolio另外,别忘了在cpt注册过程中将‘page-attribute’添加到‘support’数组中:
'supports' => array(
'custom-fields',
'page-attributes'
), 如果要更改标签“Page attributes”,则将其分配给“labels”数组中的“attributes”,即:
'labels' => array(
'name' => 'Portfolios',
'singular_name' => 'Portfolio',
'attributes' => 'Page Attributes'
),自定义帖子类型编辑器的仪表板截图如下:

发布于 2017-08-01 03:59:14
在WordPress4.7中,你可以直接从文件https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/中添加“模板”字段
https://stackoverflow.com/questions/39284513
复制相似问题