2014-02-23 138 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看到我的编辑。 –

相关问题