Prior to WordPress 4.7, you could only select a custom template from the Templates menu in the Page Attributes meta box on pages only. Since the release of WordPress 4.7, you can now add a Template drop down menu in the Page Attributes box on posts and pages for single posts types.
Previously, the Template menu was only included in the Page Attributes box on single pages.
To add the template menu to CPT’s and single post edit screens, you’ll need to add a template to your child themes root directory. The code for this is provided below.
This new WordPress functionality for post type templates enables you to use any page template on single posts and custom post types simply be selecting the template from the Template drop down menu in the box.
Here’s what the Page Attributes meta box looks like on a single post or single CPT edit page by default:
And here’s what the box looks like after adding a template in your child themes root directory:
The Page Attributes box now includes a Template drop down menu enabling you to select the template for use on any single post or CPT.
Any Post Type Template #
You can add these template headers to any template to load it on single posts, pages or custom post types :
// Template Name: Any Post Type Template // Template Post Type: post, page, portfolio
The page template also needs to include support for your different post types otherwise it won’t display the Template drop down menu in the Page Attributes box.
Here’s a basic example you can use.
Create a new file in your child themes root directory and add the code from the Gist into the file.
The above template enables you to select the page template on any page, post or custom post type named portfolio. Swap out portfolio with the name of your CPT in the above code to enable selection on other custom post types.
You can use this template as a starting point or blank page template which you can add code to.
- Using Different Custom Single Post Templates
- Load Custom Single Post Template For Specific Category & Child Categories
- How To Use A Page Template Anywhere In WordPress Using Template Include
- Custom Single Template For Genesis