Set Different Post Templates Based on ID, Tag and Category in WordPress

Print View Mobile View

WordPress Logo

I am going to start a new section on SumTips soon, for that I didn’t want the existing single post layout which, as you can see, has navigation at the top and bottom, and it also shows related posts at the end of each post.

While there are plugins like Custom Post Template that lets users use different single post layouts, I didn’t want to use any plugin.

So after a little exploring, I found a way to use Conditional Tags and Custom Post Templates in WordPress to achieve what I wanted. Using conditional tags, you can easily create and set different templates for single posts based on ID, tag and the category it is in.

Post Templates Based on Category

First up, I will show you how to style posts based on the category it is in.

Lets assume you have a category called ‘Photography’ in your blog, showcasing your spectacular captures. You can have a unique look for posts only in that category, while the rest of your posts follow the existing look and layout.

In your current WordPress theme, you should find a file called single.php. Create two copies of it and name them single-default.php and single-photography.php. We need only two post templates in this example as we are looking to style posts only in the ‘Photography’ category, and use the default single post template for all other posts in the blog.

Now open single.php in your favorite editor. Delete existing code in it and add the following:

<?php
$post = $wp_query->post;
    if ( in_category('photography') ) {include(TEMPLATEPATH . '/single-photography.php');}
    else {include(TEMPLATEPATH . '/single-default.php');
    }
?>

This will check and automatically load the correct template file when a post is requested. In our case, single-photography.php is loaded when a post from ‘Photography’ category is requested and single-default.php template for everything else. Not much to it, is there?

Do all your editing and customizations for posts from ‘Photography’ category in single-photography.php file. Leave single-default.php as it is.

Here I am using category slug to identify a post’s category, but you can also use category ID or category name.

If you have a habit of filing posts under multiple categories use this code:

<?php
$post = $wp_query->post;
    if ( in_category( array(1, 2, 3) ) {include(TEMPLATEPATH . '/single-photography.php');}
    else {include(TEMPLATEPATH . '/single-default.php');
    }
?>

The single-photography.php template would be used if the post is in either category 1, 2, or 3.

 

Post Templates Based on ID

If you want to target a single post, you can do so with the following code:

<?php
$post = $wp_query->post;
    if ( is_single( '100' ) ) {include(TEMPLATEPATH . '/single-photography.php');}
    else {include(TEMPLATEPATH . '/single-default.php');
    }
?>

Here the single-photography.php template is used only for post whose ID is ‘100’.

Target more than one post? Include them in an array:

if ( is_single( array( 100, 200, 300 ) ) ) {include(TEMPLATEPATH . '/single-photography.php');}

single-photography.php template will be used for post with ID 100, 200 and 300.

 

Post Templates Based on Tag

With conditional tags, you can also assign a single post template to a particular tag:

<?php
$post = $wp_query->post;
    if ( has_tag( 'photo' ) ) {include(TEMPLATEPATH . '/single-photography.php');}
    else {include(TEMPLATEPATH . '/single-default.php');
    }
?>

Same template for multiple tags?

if (  has_tag( array( 'photo', 'sunrise' ) ) ) {include(TEMPLATEPATH . '/single-photography.php');}

 

Multiple Single Post Templates

Now what if you want templates for multiple categories? Following our example, in addition to ‘Photography’, you also have a ‘Music’ category. For that, just add an additional check:

<?php
    $post = $wp_query->post;
    if ( in_category('photography') ) {include(TEMPLATEPATH . '/single-photography.php');}
    elseif ( in_category('music') ) {include(TEMPLATEPATH . '/single-music.php');}
    else {include(TEMPLATEPATH . '/single-default.php');
    }
?>

You can use as many templates as you want, and call them anything you like, but I’d recommend following a name structure that correlates to your category. This helps in easy identification and follows a proper structure.

That’s all! Go ahead, get creative and customize your posts.

2 thoughts on “Set Different Post Templates Based on ID, Tag and Category in WordPress”

Comments are closed.