Posted on

creating category specific post templates in wordpress

wordpress natively supports a method of creating category specific category page templates right out of the box. in fact, the entire template hierarchy is pretty flexible, and you can do some pretty crazy stuff with it. continuing with the category page example, wordgpress will look for template files in the following order, moving to the next if the last isn’t available:

  1. category-SLUG.php, where SLUG is the name of the category
  2. category-ID.php, where ID is the id of the category
  3. category.php, the default category template
  4. archive.php
  5. index.php

i can have completely different layouts for my different category pages allowing me to better present the content listings or whatever on each one, which is great. but what about the individual post templates? what if i want my posts about cats to have a giant litterbox in the background and my posts about dogs to have listings of other posts on the left instead of the right? the default template hierarchy doesn’t allow for this. here’s the order in which wordpress looks for template files where individual posts are concerned:

  1. single.php
  2. index.php

not very flexible. its almost like somebody said “screw you, you don’t need that crap for single post templates.” well, your mom. i found a solution and its pretty damned easy. first, make a couple copies of the single.php file so that you have three files total:

  1. single.php
  2. single_default.php
  3. single_cats.php, where cats is your awesome cats category slug. who doesn’t like cats?

at this point, all three of those files have exactly the same content. the next thing you’ll need to do is create a functions.php file in your theme directory if you don’t already have one. open it up and add this chunk of code in there:

// chack the category name and id and if there is a 
// single view page for that cat, use it

function THEME_get_single_cat_page() {
	global $post;	
	$cat = get_the_category($post->ID);
	$catname = $cat[0]->cat_name;
	$catid = $cat[0]->cat_ID;

	if (file_exists(dirname(__FILE__).'/single_'.$catname.'.php')) {	
	} elseif (file_exists(dirname(__FILE__).'/single_'.$catid.'.php')) {
	} else {

what this function does is first look for a template file called single_CATEGORYNAME.php, where CATEGORYNAME is the category slug for that post. if it doesn’t find that, it looks for a template file called single_CATID, where CATID is the ID of the category for that post. if neither one of those is found, it loads and uses single_default.php.

to get this to work, open the original single.php and replace everything in it with the following call:


now what will happen is every time you load a post it will first try to load your category specific single post template before going back to your default one. all that’s left for you to do now is theme your newly created template.

Leave a Reply

Your email address will not be published. Required fields are marked *