Using Different Custom Single Post Templates

WordPress includes a handy filter named single_template which you can use in a custom function.

Here’s a few examples:

Note: Please copy ALL the code from the view raw link and paste it at the end of your child themes functions.php file using a text editor like Notepad++

Single Post Template for Posts In Different Categories

This code enables you to display a custom single post template for all single posts in category-1

Download Folder

Installation Steps #

There’s only 2 steps :

Step 1 – Copy & paste the PHP code from the functions.php file to the end of your child themes functions file.

You will then need to modify the conditional tag in the code to target the category you want to customize. This line in the code will need modifying to match the category slug for your category.

if ( in_category( 'category-one' ) ) {

Example : If your category is named News with a slug news, the code will look like this :

if ( in_category( 'news' ) ) {

Step 2 – Upload the file named single-category-one.php to your child theme folder. You can then remove or add code to this file to modify the default single post template for your specific category.

Single Post Templates for Custom Post Types

This code enables you to display a custom single post template for all single pages using the custom post type portfolio.

Related Tutorials


Comments

8 responses to “Using Different Custom Single Post Templates”

  1. Hi Brad and thanks for the code. Unfortunately, it’s not working with the Breakthrough Pro theme. Have you tried it with that theme by any chance? If so, any idea why it’s not working please? Thanks.

    1. Hi Richard. I tested my code with Breakthrough Pro right now and it works. Happy to retest in your live site if you want to send me login details. Make sure the category slug and path to the custom single template match whats in the code.

  2. Brad Dalton Avatar
    Brad Dalton

    If you’re only currently using one custom post type, you can either use the WordPress Template Hierarchy to name the template for your CPT. Example single-cpt.php

    Or you can use template_include

    Or one of the code snippets in the post above.

  3. Scott Buehler Avatar
    Scott Buehler

    Brad, how would I go about adding multiple single template CPTs to this function?

    if ($post->post_type == ‘portfolio’) {

    If I tried if ($post->post_type == ‘portfolio’, ‘secondcpt’) {
    it would fail because it isn’t in an array.

    Hopefully this is quick enough to answer. Know you are busy.

    1. Brad Dalton Avatar
      Brad Dalton

      Ok. Pay the membership fee and i’ll provide the answer. http://wpsites.net/registration/

    2. Brad Dalton Avatar
      Brad Dalton

      Here’s the solution Scott

      Simply change the name of the custom post types in the array to match yours. Example: ‘portfolio’, ‘views’

      And change the file name for the single CPT template to your own as well.

      Here’s more on the use of the template_include function.

      1. Scott Buehler Avatar
        Scott Buehler

        This works perfectly. Thank you.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.