Page Templates for Custom Post Types in WordPress

For a long time now, WordPress has given theme developers the ability to allow users to assign custom page templates to pages in their WordPress sites. This allows a user to assign a different layout to a page rather than having to use the default layout. Often theme developers uses these templates to provide options without a sidebar or to have the pages sidebar of the left or right. It gives the authors of pages flexibility to choose a templates that best suites the page they are writing.

Until WordPress 4.7 was released there was no core functionality in WordPress that allows custom post types to use page templates. Now if you are developing for WordPress 4.7 or above you can use the same page template technique with custom post types and they work in the same way they do for pages.

To create a page template for a custom post type, simply add an additional line of comment to the top of the template file, below the template name, like so:

Template Name: My Custom Page Template
Template Post Types: my_post_type, movie

As you can see we declare the template name (this appears in the Page Attributes select / dropdown list) and also the post types we want this template to be available in. Thats it, you now have page templates for any post type in WordPress!

For more information on this take a look at the theme developers handbook here.

About the author

Mark is the lead WordPress developer at Highrise Digital. He has been working with WordPress for over 13 years, way back to 2005. He focuses on back-end development, integrating the website build with WordPress so it can be editable.