I have a custom post type called location. I would like to be able to use 2 different templates for this custom post type and select it via the page attributes box. I have a template set up and can select it when I'm editing a Page, but not when I am editing a custom post type. I can't find any information on how to enable this. I want to enable the dropdown select for all the custom post types, not just one.
2 Answers
Since WordPress version 4.7 Post-Type-Templates are enabled in the WordPress core.
That means that you can create multiple templates for the single post-type view as for pages.
For this you just need to edit the single post/type template, and add something like this to the top of the file:
/*
Template Name: Custom Type Template
Template Post Type: post, product, custom-type
*/
On the line Template Post Type
you just add your custom post type slug.
So this template will be available on the post
, product
and custom-type
post type.
I already answered this here.
Read more about post-type-templates of WP 4.7 here.
Ok as mmm has pointed out that I can't do the dropdown select of templates on custom post types I have put this code in my single-cpt.php
global $post;
if ( ( $post->post_type == 'location' ) && has_term( 'news', 'highlights' ) ) {
include('templates/template_feature_highlight_layout.php');
}
page
. for custom post type you have to do everything by yourself : creating the metabox in the backoffice, saving the value et generating the public page with the selected template