0

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?

  • 1
    I don't think you can combine custom template functionality with a template that's already in the hierarchy. Naming a template single-book.php without the header should result in that template being used by default, if I'm not mistaken. – Milo Apr 15 '17 at 22:42
  • Hey, @Milo! Your comment was REALLY helpful! Even though I had the file named correctly single-book.php the template was not being loaded as the default because of the header, indeed! So you got that answer right! Knowing that, I figured out the other answer straight away: if single-book.php is the only template for that CPT, there will be no post attribute section. If I want more, I can create another php template file, name it anything and THEN put that comment header... All solved! Your feedback went a long way! Thank you! – Diogo Apr 16 '17 at 5:18
  • One other point. When you make a template for a custom post type, make sure that the template e.g. single-newtype.php is located in the same folder as single.php in your theme. – AlanisTuring Sep 12 '17 at 11:16
0

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.

  • Hey, thanks for taking the time to answer my question! I appreciate it. Good tip on namespacing things! Will keep that in mind! Turns out Milo´s comment above got me going. It wasn´t a singular/plural thing. As for the template comments in the header, that´s the WP way of doing it as of version 4.7... I´ll add the full answer with details on how it was solved. – Diogo Apr 16 '17 at 5:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.