Home Forums General Issues Dedicated template for each CPT vs Single setup


Dedicated template for each CPT vs Single setup

  • I realize that title is confusing ok let me explain.
    Right now i have a site with a bunch of CPT and Custom fields. Each CPT has it’s own single-cpt.php template even though they are all pretty similar.

    When i set up that site i didn’t understand much but now that i think i do i came up with an idea of a single single.php for all similar CPT.

    It would work like this:
    The base of this setup is name all Custom Fields like this:


    Where cpt is the real custom post type slug.
    And then on the general single.php do this to display each field:

    if (get_field("afc_name_$cpt")) { 
    echo get_field("afc_name_$cpt");}

    $cpt would be the custom post type slug.
    Would this be a good setup for production?
    What i’m trying to achieve with this is not having a bunch of different single-cpt.php to maintain and just have a main one. If some things need to be unique for some of them i could include extra php files with if statements for each cpt that need it.


  • I personally like having different template files for each CPT. It simplifies code. One template field with a lot of conditionals will be more complex and harder to maintain then multiple files.

    Where the content in multiple CPTs is the same, this is what template part come in. then you can load this common template part from all of the single-cpt.php templates where the code and content will be the same.

    Get template part can even be used without any other code in the template. For example, lets say that you have 2 custom post types that will be displayed in an identical fashion. You would have two templates


    in these 2 template files you can do something like


    and then create my-common-cpt-template.php that contains all of the code that would normally be in the other 2 files.

    But like I said, when you start combining templates the code will become more complex when you need to do different things for different post types. If your goal is to make maintaining the site easier, while it does create a couple of extra files the code in those files is usually easier to understand when you haven’t looked at it in a year, and you always know what template file to start from when you have one file for each CPT.

  • Gotcha, you are right, i’ll prolly do individual files for each cpt and do template parts for the similar bits thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Dedicated template for each CPT vs Single setup’ is closed to new replies.