How to Set Up Single Post and Category Specific Templates in WordPress
There may come a time when you want to use different templates for individual posts or for entire categories. Some of the main reasons for doing this would be if you wanted to make a post without showing adsense ads or maybe to target advertising to a specific product based on the category.
For detailed information on setting up and rotating banners in your wordpress posts, you can see my post How To Add Banner Rotation To Your WordPress Blog.
Ok, let’s get started.
Create a new folder named single in your current themes folder
Your file structure will now look like
wp-content
- – - – themes
- – - - – - – your theme
- – - – - - - – - – - – - single
Upload a copy of single.php into your new single folder.
Paste the following code in at the very bottom of your functions.php right above the closing ?>
Note: Save a copy of your functions.php file before attemtping so that you can quickly restore to the default condition if this does not work out for you.
/**
* Define a constant path to our single template folder
*/
define(SINGLE_PATH, TEMPLATEPATH . ‘/single’);/**
* Filter the single_template with our custom function
*/
add_filter(‘single_template’, ‘my_single_template’);/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :if(file_exists(SINGLE_PATH . ‘/single-cat-’ . $cat->slug . ‘.php’))
return SINGLE_PATH . ‘/single-cat-’ . $cat->slug . ‘.php’;elseif(file_exists(SINGLE_PATH . ‘/single-cat-’ . $cat->term_id . ‘.php’))
return SINGLE_PATH . ‘/single-cat-’ . $cat->term_id . ‘.php’;endforeach;
/**
* Checks for default single post files within the single folder
*/
if(file_exists(SINGLE_PATH . ‘/single.php’))
return SINGLE_PATH . ‘/single.php’;elseif(file_exists(SINGLE_PATH . ‘/default.php’))
return SINGLE_PATH . ‘/default.php’;return $single;
}
Or get the code here in a text file
You may decide you don’t want any adsense ads showing on a particular post or you want to promote a specific product on this post only.
Ok, we can do that!
Note: This affects individual posts only. I will cover using different templates based on category a little further down the page.
To use a different template based upon the individual post number just make whatever changes you like to the single.php (the one in your new singles folder) and save it as single-100.php (the 100 here represents the post number.
Going by the image below, we would make our desired changes to the single.php file and name our newly modified file single-225.php to use this template for that particular post and only that post.

Find WordPress Post Number
If you want to use different templates based on category, then you will modify the single.php file and name them by their category slug or category ID, like…
single-cat-CategorySlug.php or
single-cat-CategoryNumber.php
In the image example below, we would modify the single.php file that way we wanted for all posts in the Advertising category and save the modified file as… single-cat-advertising.php

Finding Category Slug
To use the category number instead of slug, find the category number by navigating in your WP admin panel to categories and placing your mouse over the category name and looking for &cat_ID= , the number after the ID= is the category number).
In the image example below we would name our modified single.php file…
single-cat-160.php

Finding The Category Number
Note: If you are showing rotating banners or adsense ads using information from my other posts and they aren’t displaying. You can either use the full path in your modified templates to point to the files you want to include on the posts or upload the files into your new single folder since relative paths are used in the templates.
Hope this helps!
Gail
![]() |


Perfect! just what I wanted… thanks heaps bud
Awesome! This sure comes in handy, will definitely try it out and post results later.
Thanks!!
Hi, Thanks for this wonderful post, am surely going to implement on one of the blogs for one of my clients and will let you know of the results. I am pretty assured that it will work for me.
Thanks again,
Tanveer Malik
I have practiced this approach and it had worked like a charm for me.
Hey this is a most excellent trick! I was looking for something to do exactly that…
I have one piece of feedback though…I wanted to isolate a load of posts by category so i used “single-cat-MySlug.php and it worked perfectly first time.
Just as an experiment i tried to isolate a single post using single-100.php and I couldn’t make it work…just got standard template like all other posts…is this because of version changes with wp? i am using 3.01.
Also, the code to put in function.php is missing the final “}” on this page (but it is present in the text file)
Really, this was such a great help
Hi Tom, do you have an actual post #100? the number after single- needs to be an actual post number. Thanks for pointing out the missing bracket, I will get that updated.
P.S. Not sure about 3.01 compatibility, haven’t tinkered with it much yet.
Hi!
I definitely do have post #100 and I tried with a few other posts just to make sure…I think I am doing everything right…
No matter, coz the category thing was what i was after and it works a treat, but if you wanted to try and replicate my problem I am using WP 3.01 with “twenty ten” theme…
Cheers,
Tom
Thanks for coming back and letting me know. I will definitely work on that and get something up that works for the single pages and 3.01. Meanwhile, I’m glad the category targeting is working out the way you want.
Gail