Can I assign a template file to a custom post type?
I have created a custom post type called items
, and I would like to assign templates to the items like you can do with pages.
Can I assign a template file to a custom post type?
I have created a custom post type called items
, and I would like to assign templates to the items like you can do with pages.
You must be logged in to post a comment.
From WordPress version 4.7 you can now assign custom page templates to other post types along with page.
To achieve this in addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: as following.
You can get more information about it on the following pages.
https://wptavern.com/wordpress-4-7-brings-custom-page-template-functionality-to-all-post-types
https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
You can create template for custom post types by creating files, like for example :
See Template Hierarchy in the codex.
PS : this has already been answered.
Here’s what works for me:
Thus given a post of custom post type
foobar
and a slug ofhello-world
the above code would load thesingle-foobar-hello-world.php
template, if it existed.For those reaching this thread through Google, WP 4.7 introduced templates for all post types. See Make WP Core for a full walkthrough. You’re no longer restricted to one template for all of your CPT, you can assign individual templates post by post just like you can do with Pages.
This is a little old but you can also try this:
Create a template for the custom post type:
The file should check the slug and verify if a file exists, if not, fallback to a default template file:
Replace all instances of custom-post-type-slug with the actual slug of your custom post type.
I do this for ease of use and organizational purposes. It seems cleaner to me than having all of the files in the theme’s root folder.
example folder structure:
This is very simple to do.
Create a new PHP file in your theme root directory and add this to the top:
Full example will be as following:
First create on page named as Items as your wish which display the content from items post types, than create one template file as below and named that template-items. Select that template for the page you have created.
We worked out another solution using the custom post template plugin