Custom Post Type Templates for WordPress

Published: June 3, 2016  by 
WordPress Custom Post Type Templates One common complaint I have observed on the support forums is templates for Custom Post Types. They simply don’t exist and there is no plan to support them. For example, if you have a post type with the name of gallery, the recommendation is to create a PHP file in your theme called single-gallery and code out that template. However, what if, like pages, you could have a template and have the flexibility of templates within your Custom Post Type? In this tutorial, I’m going to show you how I created WordPress page templates for Custom Post Types.

Modifying the WordPress Theme

In this example, I’m going to create a child theme of Twenty Twelve. To follow along, you can download the Twenty Twelve Child Theme. First, let’s activate the child theme.
Twenty Twelve Child Activated
Twenty Twelve Child Activated
Here is the directory structure I used to allow post type templates:
Twenty Twelve CPT Structure
Twenty Twelve CPT Structure
I created a folder called cpt-templates and within that folder, I created a folder for each post type: book and gallery. These folders must match the name of the post types you are planning to create. Within each post type folder, I just copied the full-width.php file into each directory as an example of what’s possible.

The Plugin

We’ll be creating a plugin called CPT Templates. You can download CPT Templates to follow along with this tutorial.
CPT Templates Top-Level View
CPT Templates Top-Level View
CPT Plugin Folder Inside
CPT Plugin Folder Inside
Plugin Activation
Plugin Activation
One you have downloaded and activated the plugin, you should see two new post types: Book and Gallery. Assuming you have the child theme activated and the plugin activated, you should now be able to create a new book or gallery and select a page template for both.
CPT Template Save
CPT Template Save

Plugin Code

Let’s dive into the plugin code and explain how this all works.

Plugin Initialization

First, the plugin initialization and initial hooks:
The above code initializes the plugin and sets some post types in a class variable.

Post Type Creation

The above creates the post types and registers a meta box callback.

Template Selection

This method is magical in that it scans your theme folder based on post type and returns which templates can be selected.

Template Rendering

The above is called from the template_include hook. It searches the current object for a template, and if it exists, uses it instead of the standard WordPress template.

Template Saving

The above saves the post type as post meta. We use the standard meta keys to be consistent with the naming convention that WordPress uses.

Scanning Functionality

I stole the above from WordPress Core. There wasn’t a way to call it directly, so I chose to include it in the plugin.

Conclusion

Gallery With No Template Selected
Gallery With No Template Selected
Gallery With Template
Gallery With Template
As shown above, a Gallery with a template is full-width, while a Gallery with no template shows the default with a sidebar. You can get as crazy as you want with the above solution, and even start assigning templates to posts if you feel so inclined. If you have any comments or questions, we’d love to hear from you. Feel free to reach out to us here or via social media and let’s get a conversation rolling! Be sure to also read: How-To Create WordPress Custom Post Type Archive Content With CMB2