WordPress Page Templates for Custom Post Type

I have a custom post type, “Store Pages” for instance, which is an almost identical duplicate of the default WordPress “Page” post type.

Like the “page” post type, I would like to create page templates (not post-type templates) and be able to select them from the “Template” drop-down within the “Page attributes” box in the page editor.

Read More

I have created several templates, but the drop-down menu does not appear; I am assuming this is because a custom post types does not allow support for this.

Is there a way I can create page templates for a custom post type without using “single-{post-type-name}.php” and having a dozen queries to load up different template files?

I have double checked the comments for the templates are correct as they appear when I create a new page (post type, “Page”).

Help would be greatly appreciated.

Related posts

2 comments

  1. If I understood correctly you want a Select template dropdown for your custom post type.
    You can do this easily through Advanced Custom Fields, here’s a quick guide how to get through.

    After installing the plugin you can access the Custom Field section, if you open it and click Add new it bring you to the field editor. Name it whatever you want, it’s just for administrative display.

    On Field type choose “Select”, this will allow you to construct a select box on your backend, keep in mind the value of “Field name” you will need this later on the code.

    Create new field group

    Scrolling down you can add the values of the select box in the following format: "key value : Textual label" just assume for now you want 2 templates, one for audio posts and one for video posts.

    Key values for the select

    If you keep scrolling down you can see the display rule for this field group, now you will have “post” and “page” by default, when you add different content types you will have the additional content types here to select, just go ahead and pick yours.

    Post rule selection

    And, ta-da. If you go on the custom content type edit window you will find your new fresh select box here waiting for you.
    Select box on the post window

    The code integration now is very simple, just go onto your single-{post-type-name}.php template and pull the custom field data into your loop. Then you can use this to use get_template_part() to pull your custom templates.

    <?php $template_type = get_field('template'); // This must match with the field name value ?>
    
    <?php if (isset($template_type) && !empty($template_type)): ?>
        <?php get_template_part( 'store', $template_type ); ?> 
    <?php else: ?>
        // You should have a fallback for the all the existing posts without template set or if you create a new post without a template.
    <?php endif; ?>
    

    In this specific example the pulled template files will be in the format of store-{key-value-of-the-selectbox}.php, of course you can readapt that for you best convenience.

Comments are closed.