Support

Account

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

  • 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. https://developer.wordpress.org/reference/functions/get_template_part/ 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

    
    single-cpt1.php
    single-cpt2.php
    

    in these 2 template files you can do something like

    
    <?php 
      get_template_part('my-common-cpt-template');
    ?>
    

    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.