How to set a default CPT template and create post attribute´s display rules?

I have registered a new post type and made a custom template to display it. According to WP´s latest support for CPT templates, all it takes to make it available for any given CPT is to place this header on the template file:

/**
 * Template Name: Single Book
 * Template Post Type: book
 */

Then, following the template hierarchy, I named my template single-book.php. And alright, now I have this template available for the 'book' post type. It works. Cool!

However, for some reason, I still need to manually select it within the post attribute section every time I create a new book as the default selection is the standard template for posts. (when the default should be my custom template after naming it as stated above, right? Unless I´m missing something...)

So, the way it is, if by any chance I / the user forgets to change it, the CPT will obviously NOT display properly on the front-end as it isn´t using the template made for it.

I must prevent that from happening and, as I´ll be working with plenty CPTs, I need the ability to choose from 2 approaches when registering a CPT:

1- Determining which template is selected by default on the post attribute section. (ideal for CPTs that can use a range of custom templates depending on the circumstance, but making sure the default selection is appropriate and NOT any other standard template);

2- Making the post attribute section DISAPPEAR for CPTs that should ONLY use a specific custom template. (while making sure that´s the one being used).

I´ve done extensive research and couldn´t find anything at all on how to go about this.

Yet, I suspect this not only can be easily achieved without much hassle but also that it has to do with some parameter within the post type array when registering it.

I´m not sure, but maybe something along the lines of:

add_theme_support('custom-post', array (
    'book'= array (
       'singular' = ... ,
       'plural' = ... ,
       'supports' = array(...),
       'some-parameter-to-set-default-template' = ... ,
       'some-parameter-to-disable-post-attribute-selection' = ...

    ),
) );

Can anyone help?

Topic post-type-support post-type custom-post-types Wordpress

Category Web


Thanks to Milo I managed to solve both of my needs. His comment answered one of them and opened up the way for me to figure out the other one.

So, if I want only ONE custom template for any given CPT, all there is to it is the template hierarchy. No need to insert that comment in the header, like I was doing. In my case, the CPT was registered as book, so the template file was named single-book.php.

Now, upon creating a new book, there will be no post attribute section and it will be using the correct template. (which is what´s expected... the comment section in the header was messing this up).

However, If I want more custom templates for the same CPT that´s when that comment section in the header comes in to play.

I can then name the new template file literally whatever I want like-really-whatever-I-want.php and address it to the appropriate post type by following this structure:

/**
 * Template Name: Whatever you wanna call the template
 * Template Post Type: book
*/

Doing that will bring up the post attribute section when adding a new book. The default template will still be the one previously created following the template hierarchy, but now there will this second one to choose from.

I can rinse and repeat this process to get as many templates as I want for any given CPT.

And if I want one of these extra templates to be used by more than one CPT, I can do so by simply adding the other post type names next to the one I started with. Like so:

/**
 * Template Name: Whatever you wanna call the template
 * Template Post Type: book, event, game, movie
*/

All turned out to be very, very simple. Yet, so powerful. Hope this may help someone else.


Try naming it single-books.php. Normally when you register a post type it's plural. Use the exact wording you registered the CPT with in your register_custom_postype.

I like to use namespacing to avoid these issues all the same. SO register wpse_books as the post type and then your single would be single-wpse_books.php and archive would be archive-wpse_books.php

there is no need to set a parameter to do this as wordpress will find the single/archive files based on the post name automatically.

You shouldn't be adding the template names at the heads either. You can comment what it's for but don't say template as this could confuse things later.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.