How to get template drop down menu in page-attributes of custom post type?

admin2025-06-07  49

When I register my custom post type, I set this:

'hierarchical'   => true,
'supports'       => array( 'title','author', 'page-attributes' ),

So, I am supose to see 'order', 'templates', 'parents' in Attributes box when create new post. But, I don't see the 'templates' drop down showing up. Anything else I should do to enable the choice of 'templates' ?

When I register my custom post type, I set this:

'hierarchical'   => true,
'supports'       => array( 'title','author', 'page-attributes' ),

So, I am supose to see 'order', 'templates', 'parents' in Attributes box when create new post. But, I don't see the 'templates' drop down showing up. Anything else I should do to enable the choice of 'templates' ?

Share Improve this question edited Dec 1, 2011 at 3:02 Jenny asked Dec 1, 2011 at 2:50 JennyJenny 1,7674 gold badges27 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

You can't apply templates to custom post types in this manner. That will show up only if the post type is 'page' ( Check the wp-admin/includes/meta-boxes.php line 568 ). However if you want to style all your single custom post types in the same manner but different from other post types you could use the single-[posttype].php -> http://codex.wordpress/images/1/18/Template_Hierarchy.png

Well, as of Wordpress 4.7 custom templates are also available to custom post types, when defining a template, below the name of the template add another line like (where 'product' is your custom post type):

<?php
/*
Template Name: My custom layout
Template Post Type: post, page, product
*/

// your code here

and remember to add 'page-attributes' when registering your custom post type:

'supports' => array('title', 'page-attributes'),

to display the "Post attributes" box.

With my theme, I provide "virtual" templates. There are no specific {template}.php files in my theme, so I filtered the PAGE templates like so:

function my_virtual_templates( $templates ) {

    $my_virtual_templates = array(
        'virtual_template_id_1' => 'Template 1',
        'virtual_template_id_2' => 'Template 2',
        'virtual_template_id_3' => 'Template 3'
    );

    // Merge with any templates already available
    $templates = array_merge( $templates, $my_virtual_templates );

    return $templates;
}

add_filter( 'theme_page_templates', 'my_virtual_templates' );

I was looking for a "simple" way to add the actual post meta box on a Custom Post Type (CPT) when I came across this post. Since my new CPT will use this same array of "virtual" templates, I just needed to get a post meta box in place.

Using the theme_{$post_type}_templates It automatically creates this post meta box area for me. So where my CPT is called my_cpt I added the filter like so:

add_filter( 'theme_my_cpt_templates', 'my_virtual_templates');

Now, the meta box and selector shows up, and I can even change on the bulk edit screen since this is all built in. Very handy!

just create any template file and set in header of template this:

/*
Template Name: Some Name
Template Post Type: your_type, page
*/

then template selector appears in 'Post Attributes'

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749238817a317523.html

最新回复(0)