Wordpress Custom Post Type Template

admin

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.

Custom Post Type – Template Hierarchy # WordPress will work through the template hierarchy and use the template file it comes across first. So if you want to create a custom template for your acmeproduct custom post type, a good place to start is by copying the single.php file, saving it as single-acmeproduct.php and editing that. The “Custom post types” plugin generates various IDs, you can use these IDs to create / use in your own custom functions, custom templates and more. This plugin is written using only functions included in the WordPress core, so it does not reduce the performance of your website and does not cause compatibility problems. 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.

However, what if, like pages, you could have a template and have the flexibility of templates within your Custom Post Type?

Create An Archive Page Template For A Custom Post Type Elementor’s Archive template can be used to setup archive pages for any custom post types. To create a custom post types archive page that displays custom fields, you need to use Elementor’s Custom Query Filter functionality. Bear in mind that this is oriented towards advanced developers. “How to create custom templates for custom post type in WordPress”, is one of the most popular questions from the internet. Before we build the custom template for the custom post type, you should understand how WordPress decides which template file will be used on individual page.

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.

Wordpress Custom Post Type Supports

To follow along, you can download the Twenty Twelve Child Theme.

First, let’s activate the child theme.

Here is the directory structure I used to allow post type templates:

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.

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.

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

Wordpress custom post type custom page

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

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

WordPress 4.7 brings a new feature in the ability to add a Custom Post Type template in a similar way a Page Template is added.

A page template can be added by creating a specific file page-{id}.php or page-{slug}.php file, the problem with this approach is that it is not reusable for other pages, so a more global template can be create by also creating a php file in your themes folder with the following header.

As long as the header info is in place you can name the file what you like but try and keep some sane naming conventions for easier file management.

Wordpress Post Template


Then the template is available under from the post editor under the Page Attributes > Template metabox.

To add a Custom Post Type template in WordPress 4.7 you add in an extra line Template Post Type to the header info and add in the post type name, either a regular post or the registered name of the Custom Post Type.

Little failure. So now the template is available to not only posts and pages but also to CPTs Portfolio and Event

One caveat is that on versions of WordPress pre 4.7, the templates will show under the Page template list – this can be hidden using the theme_page_templates filter…

Wordpress Custom Post Type Template Not Working

Ref & Ref