这对我来说很有用,所以我不确定为什么它现在不能正常工作。如何将页面附加到自定义文章类型
我创建了一个自定义后类型:
add_action('init', 'register_team');
function register_team(){
$args = array(
'label' => __('Design Team'),
'singular_label' => __('Design Team'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
'supports' => array('title', 'editor', 'thumbnail'),
'add_new' => __('Add New Member'),
'add_new_item' => __('Add New Member'),
'edit' => __('Edit Member'),
'edit_item' => __('Edit Member'),
'new_item' => __('New Member'),
'view' => __('View Member'),
'view_item' => __('View Member'),
'search_items' => __('Search Design Team'),
'not_found' => __('No info found'),
'not_found_in_trash' => __('No info found in Trash'),
'parent' => __('Parent Info'),
'menu_position' =>__(7),
);
register_post_type('team' , $args);
}
,并呼吁,我可以在CMS看到,添加新条目,等我需要一个页面模板附加到这个自定义后类型的功能。在同一个网站上,我创建了一个名为陈列室的自定义帖子类型,并通过创建名为page-showroom.php的文件将自定义帖子类型附加到页面上。但是,当我创建一个名为page-team.php的文件时,它不会关联到此页面。这是一个语法问题吗?
UPDATE 我通过在CMS中创建页面并使用页面属性添加模板来解决此问题。我不特别喜欢这个解决方案的原因是由于用户可能更改页面模板的可能性,导致它不再起作用。
我只是觉得我缺少一些相对于WP核心如何定义页面?可变模板的名称,或者是一个错字,愚蠢的错误,等等
UPDATE 按照要求,这里是其中的functions.php加载所有的我CPT的
// CUSTOM POST TYPES
add_action('init', 'register_showroom');
add_action('init', 'register_project_gallery');
add_action('init', 'register_slideshow');
add_action('init', 'register_team');
// ADD Showroom
function register_showroom(){
$args = array(
'label' => __('Showroom'),
'singular_label' => __('Showroom'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "showroom",'with_front' => true), // Permalinks format
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'page-attributes'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New'),
'edit' => __('Edit'),
'edit_item' => __('Edit'),
'new_item' => __('New'),
'view' => __('View'),
'view_item' => __('View'),
'search_items' => __('Search Showroom'),
'not_found' => __('No info found'),
'not_found_in_trash' => __('No info found in Trash'),
'parent' => __('Parent Info'),
'menu_position' =>__(4),
);
register_post_type('showroom' , $args);
}
// ADD Project Gallery
function register_project_gallery(){
$args = array(
'label' => __('Project Gallery'),
'singular_label' => __('Project Gallery'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
'supports' => array('title', 'editor', 'thumbnail'),
'add_new' => __('Add New'),
'add_new_item' => __('Add New'),
'edit' => __('Edit'),
'edit_item' => __('Edit'),
'new_item' => __('New'),
'view' => __('View'),
'view_item' => __('View'),
'search_items' => __('Search Project Gallery'),
'not_found' => __('No info found'),
'not_found_in_trash' => __('No info found in Trash'),
'parent' => __('Parent Info'),
'menu_position' =>__(5),
);
register_post_type('project_gallery' , $args);
}
// ADD Slideshow
function register_slideshow(){
$args = array(
'label' => __('Homepage Slideshow'),
'singular_label' => __('Homepage Slideshow'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
'supports' => array('title', 'excerpt', 'thumbnail'),
'add_new' => __('Add New Slide'),
'add_new_item' => __('Add New Slide'),
'edit' => __('Edit'),
'edit_item' => __('Edit'),
'new_item' => __('New'),
'view' => __('View'),
'view_item' => __('View'),
'search_items' => __('Search Homepage Slideshow'),
'not_found' => __('No info found'),
'not_found_in_trash' => __('No info found in Trash'),
'parent' => __('Parent Info'),
'menu_position' =>__(6),
);
register_post_type('slideshow' , $args);
}
// ADD Design Team
function register_team(){
$args = array(
'label' => __('Design Team'),
'singular_label' => __('Design Team'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
'supports' => array('title', 'editor', 'thumbnail'),
'add_new' => __('Add New Member'),
'add_new_item' => __('Add New Member'),
'edit' => __('Edit Member'),
'edit_item' => __('Edit Member'),
'new_item' => __('New Member'),
'view' => __('View Member'),
'view_item' => __('View Member'),
'search_items' => __('Search Design Team'),
'not_found' => __('No info found'),
'not_found_in_trash' => __('No info found in Trash'),
'parent' => __('Parent Info'),
'menu_position' =>__(7),
);
register_post_type('team' , $args);
}
所以我的代码可以成功创建一个page-showroom.php,page-project_gallery.php,single-project_gallery.php,single-showroom.php自动附加到正确的CPT,但是如果我创建page-team.php,它只是加载页面.PHP。
这里是页showroom.php的样本,其工作原理:
<?php /* Template Name: Showroom */ ?>
<?php get_header(); ?>
<div id="primary" class="site-content showroom">
<div id="content" role="main">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', 'showroom'); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php get_footer(); ?>
和页面team.php,不工作
<?php /* Template Name: Team */ ?>
<?php get_header(); ?>
<div id="primary" class="site-content team">
<div id="content" role="main">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', 'team'); ?>
<?php //comments_template('', true); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php get_footer(); ?>
您是否曾***拥有支持页面模板的CPT?我的意思是,它显示了属性元框中的模板选择? [查看核心文件](http://core.trac.wordpress.org/browser/tags/3.5.1/wp-admin/includes/meta-boxes.php#L641)*这是不可能的*。无论如何,您应该将'page-attributes'添加到''supports''键,尽管这只在'Attributes'元框中显示'parent'和'order'。 。 。 。 。 。 。 。 。 。 。 。 。 PS:很好的问题! – brasofilo 2013-03-13 19:59:07
我确实创建了一个名为showrooms的CPT(完全相同的方式),然后创建了一个名为page-showrooms.php的页面,并将该模板附加到CPT,而不必将其应用于页面属性框下,因此它似乎是可能的。可能是侥幸或错误,但可能。 – 2013-03-13 21:18:45