WordPress: Multiple Custom Templates for Posts and Custom Post Types.

By -

In this short article i will show you how you can add multiple custom template files for posts and custom post types. This feature was already added to WordPress in version 4.7 but it’s still a lesser know feature of theme development.

Templates for WordPress themes.

By default WordPress supports a large list of template files for your themes but these files have limited support for multiple variations. For posts you are basicaly limited to the single.php file, and for custom post types there is the single-{post-type}.php.

Custom templates for WordPress pages.

Adding multiple custom templates to WordPress pages has long been possible by adding a template header like below to your template files like below. But this was only supported by pages and doesn’t work for the default posts or custom post types.

Custom templates for Posts and Custom post types.

As of WordPress 4.7 there is a new lesser know feature that enables you to add multiple templates to posts, pages and custom post types.

To test this new feature we will first have to register a custom post type. Copy the code below to your functions.php file to create a new custom post type called “product”

There are a lot of options when creating custom post types, which you can play with, but for our purpose we will keep it simple. Do make a note of line 12. There you can toggle between the Gutenberg and Classic editor. At the time of writing by default the classic editor will be enabled for custom post types and line 12 will enable the Gutenberg editor.

The code above will register the new custom product post type which will show up in de WordPress admin like shown below.

custom post type in wordpress admin

Create templates for Posts and Custom post types.

To create a template for our new product post type we can create a single-product.php file and add a layout to it. This is great but what if we need a different layout for a specific product?

Well… Like i said… As of WordPress 4.7 you can make use of a new special comment header in your template files.

Create a new file in your theme directory called full-width-page-layout.php and at the top add the code snippet below.

This will register the new template for use in posts, pages and the custom product post type. And it wiil show the “Post atributes” box on the right side of your edit pages for both the classic and the Gutenberg editor as shown below.

select emplatein wordpress admin

Note you can change the value of Template Post Type: to any registered post type. So if you only want a template to be used with the Products post type change the header like shown below:

As simple as this may seem, it’s a very powerfull and long requested feature, and i hope this little article will help you create even more amazing themes for your WordPress websites.

Happy templating 🙂

If you have any suggestions please do let me know! You can get in touch via Twitter: Vanaf1979.

  • WordPress Development
  • WordPress
  • Tutorial
  • Php