2013-03-13 94 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