Templates For Custom Post Types and Posts

Altough WordPress is a great platform with many features, it doesn’t provide us with the ability to select a page template for Custom Post Types or just single blog posts. I needed that feature for my own website so I decided to make a small and efficient plugin to do so. In this post I will explain how you can get this feature.

The first step is to add the following code to your themes functions.php

This script checks if there is a custom field set on a post and includes the template. If you can’t see a box with custom field below a post (in the post editor) select screen options in the top right corner and click on custom fields. Then, you can add a field called ‘template’ (you might have to write it yourself instead of selecting it via the dropdown menu. This field can contain either the name of the template, or the relative path to the template.

I decided to no include a selector widget for various reasons:

  1. I wanted to keep this plugin fast, light and efficient
  2. It might mess up the layout for custom post types already adding widgets
  3. It should be that hard to figure out which page templates the theme supports.

You can find all the page templates which the theme supports by adding a new page.On the right side there should be a box called page attributes and a drop down menu containing all registered page templates.