How do I assign a custom post to a specific WP template

WordPress 6 posts 2 voices

  1. Hi Justin,

    I’ve been trying to figure out for some time how I can force a Custom Post to use a custom Post Template. It seems to me that if you use a Custom Post type you should not have to choose its template in the WP Post Template drop down every time you add a new post – which I think is what you are saying here (just ran across this today). https://core.trac.wordpress.org/ticket/12759

    Is there a code I can put in functions (or theme.php in my case in hybrid base) so that if I have a Custom Post called NPWG it will use the WP Post Template: NPWG?

    Thanks
    Charles

  2. If you’re using Hybrid, you can create a singular-yourposttypename.php template, which will always be used for that post type.

  3. What do I name it? If I use
    ‘/*
    WP Post Template: NPWG
    */’

    it creates an error.

  4. You don’t put anything like that in the file. It’s based on the file name. So:

    singular-yourposttypename.php

    yourposttypename should be replaced with your exact post type name as used in register_post_type().

  5. Got it – once I figured out it’s single-yourposttypename.php – not singular. Singular is the definition term.

    Thanks
    Charles

  6. Sorry about that. I forgot that I changed that to match core WP when core WP finally caught up with Hybrid Core, which had used singular- for years.

    Yes, it should be single-.