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

- – - –  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’;


* 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

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

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 Slut

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…


Finding The Category Number

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!


Additional Articles From "Technical"



Thanks, Gail, great post! I got a little confused around the ‘how to count the cloaked links’ section but I think that was more due to the time (3am) than anything else. I have tried your methods again this morning though and they work perfectly.

I’m using it on an unindexed landing page, driving traffic via PPC, and promoting a clickbank product. So far so good…


Glad you were able to get it going! Your problem with understanding probably had less to do with it being 3am when you were reading and more to do with it being 3am when I wrote it. ;)


Hi Gail,

I tried your cloak link for clickbank, and it worked perfectly on the first time. the same cloak link can be used for commission junction as well?. I looked at your cloak link generator, not sure I understood it well, to give it a try.
thanks for your help Gail.

Warm regrds


Hi Charles,

Glad you were able to use the clickbank code easily. I don’t use CJ much and haven’t tried to see if I could cloak the links. For the misc items I do promote, I use my own url shrinker. You can see it at , you are free to use that one if you like or you can install the script and use on your own site. You can download it at , I also put a link to it in the freebies section. It’s easy to install and the readme file is included. If you have any problems with it, just let me know. On a side note, I think it would not be difficult to do Ebay links and I’ll look at that sometime soon.

Take care,


Hi Gail,

Thank you so much for the reply. the url shrinker seems attractive. No Gail I did not use it yet, though just tested it. Just saw your reply, thought I will say thanks. will catch up with you once I try it out. Thanks again Gail.

Warm Regards


hi nice post pretty useful I got this to work, although it would be nice to know how to also include .html extension on the end instead of just I’m sure this can be all done in .htaccess file just not sure I’m a little bit of a green horn lol!


Hi Sheldon,

It’s no problem to add the .html to the end.

RewriteRule ^(.*)

RewriteRule ^(.*)\.html

Then your link would look like

This works with clickbank as well. And you can change the html to any file extension you want. It is basically just ignored anyway.



thanks for useful tips you let me to know more about .htaccess.
how i can indexing my newly created sites using .htaccess in search engines like google?

thanks alot for article.


Whoa… this site is pretty awesome :) your layout is really well designed, and your blogs are (judging from what i’ve read) very interesting. heehee… consider yourself favorited. :-P


Hello, I enjoy reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the better of luck for all your blogging efforts.

Flippa Chick

Excellent post! I’ve switched over one of my dynamic websites to a static .html website and was looking for a more efficient way to cloak my Amazon links since I couldn’t use the features of plugins.

Thanks for sharing! :)


Amazon Links idea great. I’m going to try it with my Amazon affiliate sites. thank you.

Share Your Thoughts