Support » Developing with WordPress » Custom selectable single template for the “post” post type

  • Resolved nicole2292



    I have a need to make a custom single template for the “post” post type in wordpress so my client can choose between two different single layout templates each time they create a new blog post.

    I am very familar with the wordpress template hierachy and I know how to do this for a specific post using single-{post-type}-{slug}.php, however this template needs to be assigned by the client for multiple posts. I want this to function exactly like a custom page template where it can be selected in the “Template:” dropdown under Page/Post Attributes when editing the page/post.

    I have created my custom template file single-alternative.php and placed the following php comment at the top of it /* Template Name: Alternative Template */ as you would do for a custom page template. This template is working correctly as it’s showing up for selection under “Page Attributes” when editing a page.

    I have also added the “Post Attributes” meta box to the post edit page using the following code:

    add_post_type_support( 'post', 'page-attributes' );

    However for posts only the order appears and not the dropdown to select the template file.

    I note that this has been asked before here:

    I have read the response and reviewed all the links provided and they do not seem to actually answer the question. They are talking about adding custom post meta and custom fields to the Page Attributes meta box and I don’t want to add custom post meta or custom fields in this case. I just want to select the template for the single post exactly like I would for a single page. Given both pages and posts are just post types in wordpress this functionality should work similarly for both.

    Lastly I understand that pages are hierarchical and posts are not by default. I believe that in order for page templates to be selected a post type needs to be hierarchical. Therefore I added the code snippet found here in an attempt to make “posts” hierarchical so I could select the post template under attributes however that also did not work. Nothing new showed up under attributes, not even the parent box, so I don’t think posts became hierarchical.

    Can anyone help?

    Thanks very much,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Nicole,

    Did you remember to declare support for the POST post type in your custom page template file? While page templates now work for all post types (before 4.7 it only worked for the PAGE post type), by default, a custom page template will be available to the PAGE post type only… until you declare support for additional post types, eg:

    Template Post Type: post, page, event

    See the documentation below. And no, hierarchical support has nothing to do with this. If the post type has hierarchical support, the Parent/child field will be presented, but it is NOT required to use custom templates for any particular post type.

    Thread Starter nicole2292


    Thank you so much George!

    That was the missing piece of my puzzle 🙂

    I hope this post helps others looking for the solution to this problem.

    Note: yes it was not necessary to make the posts hierarchical.

    I’m glad you got this sorted out.

    I hope this post helps others looking for the solution to this problem.

    I also hope so, though people are not likely to know this solved your problem unless you mark your post as RESOLVED 😀

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom selectable single template for the “post” post type’ is closed to new replies.