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

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 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…

single-cat-160.php

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!
Gail

Did You Find This Post Helpful?
Support this author by adding to your favorite bookmark site now. Thanks!

  • Digg
  • StumbleUpon
  • Twitter
  • Mixx
  • Yahoo! Buzz
  • BlinkList
  • del.icio.us
  • email

2 Responses to “How to Set Up Single Post and Category Specific Templates in Wordpress”

  1. Perfect! just what I wanted… thanks heaps bud

  2. Awesome! This sure comes in handy, will definitely try it out and post results later.
    Thanks!!

Leave a Reply