2014-02-23 139 views
3

我正嘗試在自定義帖子類型中使用自定義頁面模板。但是,嘗試添加新頁面時,模板選項不會顯示在帖子類型中。請參閱下面的代碼:Wordpress自定義模板在自定義帖子類型中不顯示

的functions.php自定義職位類型:

add_action('init', 'register_cpt_product'); 

function register_cpt_product() { 

    $labels = array( 
     'name' => _x('Products', 'product'), 
     'singular_name' => _x('Product', 'product'), 
     'add_new' => _x('Add New', 'product'), 
     'add_new_item' => _x('Add New Product', 'product'), 
     'edit_item' => _x('Edit Product', 'product'), 
     'new_item' => _x('New Product', 'product'), 
     'view_item' => _x('View Product', 'product'), 
     'search_items' => _x('Search Products', 'product'), 
     'not_found' => _x('No products found', 'product'), 
     'not_found_in_trash' => _x('No products found in Trash', 'product'), 
     'parent_item_colon' => _x('Parent Product:', 'product'), 
     'menu_name' => _x('Products', 'product'), 
    ); 

    $args = array( 
     'labels' => $labels, 
     'hierarchical' => true, 
     'description' => 'Product pages', 
     'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes'), 
     'taxonomies' => array('category'), 
     'public' => true, 
     'show_ui' => true, 
     'show_in_menu' => true, 
     'menu_position' => 5, 

     'show_in_nav_menus' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'has_archive' => false, 
     'query_var' => true, 
     'can_export' => true, 
     'rewrite' => true, 
     'capability_type' => 'page' 
    ); 

    register_post_type('product', $args); 
} 

頁面模板(除模板標記的)

<?php 
/* 
Template Name: Product 
*/ 
?> 
+0

你想達到什麼目的?什麼是您的模板的文件名? –

+0

模板是single-product.php。我只是試圖在自定義頁面類型中使用此模板。 –

回答

3

你不能有任何的WordPress模板的頁面模板文件:

像:

archive-*.php 
category-*.php 
single-*.php // in your case 

或任何其他。

但你可以有page-*.php或頁面模板定製filename.php

any-file-name.php 
page-*.php 

重命名single-product.php文件到別的東西,或只是刪除single並重新命名爲別的

像:

my-single-product.php // this should work 

切記single-product.php將用於呈現您的自定義帖子類型(產品)單個帖子內容。

編輯:

頁模板只爲WordPress的內置頁面。自定義帖子類型應該只有一個模板。如果你有需要改變它,那麼你很可能會想創建另一個帖子類型或只是使用頁面或常規帖子。

來源:Page Attributes options for custom post types

+0

非常感謝您的回答。我已經重命名了該文件,並且仍然存在相同的問題。以前,模板使用標準頁面(並在頁面上顯示),但它既沒有顯示在我的自定義帖子類型(現在或之前)上。因此,我認爲定義帖子類型更重要。歡呼 –

+0

例如該模板不能從帖子屬性選項中選擇。 –

+0

@PhilHudson看到我的編輯。 –

相關問題