2013-03-13 88 views
3

这对我来说很有用,所以我不确定为什么它现在不能正常工作。如何将页面附加到自定义文章类型

我创建了一个自定义后类型:

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(); ?> 
+0

您是否曾***拥有支持页面模板的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

+0

我确实创建了一个名为showrooms的CPT(完全相同的方式),然后创建了一个名为page-showrooms.php的页面,并将该模板附加到CPT,而不必将其应用于页面属性框下,因此它似乎是可能的。可能是侥幸或错误,但可能。 – 2013-03-13 21:18:45

回答

1

我通常只是遵循模板层次结构。在你的情况 - 我假设你想让你的页面列出post type = team的所有帖子 - 这意味着在你的“page-templates”目录下创建一个名为“archive-team.php”的页面。或者,如果您只想显示单个帖子,则可以使用“single-team.php”。至少,这是你应该这样做的方式。我这样做,它适用于我。

http://codex.wordpress.org/Template_Hierarchy

+0

对,页面模板不能以同样的方式工作?我成功地为单人艺术家创作了一个模板,效果很好,就像页面展示厅一样。那么这是命名,团队的问题吗?我知道你应该远离一些话,但我不认为“团队”会是一个。 – 2013-03-19 15:01:24

+0

嗯......我需要看到更多的代码才更有用。如果可能,你可以发布single-artists.php,single-team.php的完整代码以及两种自定义帖子类型的代码吗?我很确定“团队”不是保留字。我认为这是代码中的东西。 – vtacreative 2013-03-19 19:22:55

1

您需要的功能之前添加add_action('init', 'register_team');

+0

包含在我的函数顶部。如果该行不存在,它将不会填充到CMS中。我认为这是一个命名问题。 – 2013-03-13 18:51:30

+3

为您的自定义帖子类型和单独的单个文件创建循环循环文件以显示单个自定义帖子类型内容。 – 2013-03-24 12:37:53