How to: Use different single template per category

Why a blog must always have the same layout? In this recipe, I’m going to tell you how you can define differents post layouts for each of your categories.

First, you'll have to create each different layouts you'd like to use. In this recipes, they're named single1.php and single2.php. The default posts template is single_default.php.

Once you have your templates ready, insert the following code on your regular single.php file. When single.php will be called, this code will automatically include your custom layout based on the current category id. If you need help for getting category ids, juste read this recipe.

<?php
  $post = $wp_query->post;

  if (in_category('1')) {
      include(TEMPLATEPATH.'/single1.php');
  } elseif (in_category('2')) {
      include(TEMPLATEPATH.'/single2.php');
  } else {
      include(TEMPLATEPATH.'/single_default.php');
  }
?>

8 Responses

Nov 09 2008 08:53

This is what I’m looking for. I can make it now. Thanks a lot :-)

Nov 09 2008 09:43

Another option that might be of interest to people is a plugin I have written - Idealien Category Enhancements - which allows you to tie category template and single / post template together based on a selection made by managing the category.

It is useful for those who use Wordpress as more than a blog and have unique sections of content and want to have correspondingly unique layouts (portfolio versus blog versus gallery versus contact list, etc). You aren’t tied to the selection loop that is functional in this example, but hits a threshold when you’re creating lots of nested categories and want to have a particular category / single template be used for all. It’s one of those hard to explain but once you play with it when designing it makes a lot of things a lot easier.

Nov 09 2008 10:26

@JamieO: Thanks for the plugin! It seems really cool:)

Nov 10 2008 07:15

Or you could just do a different category page for each one, naming it category-##.php (i.e. category-6.php)

Nov 10 2008 16:14

@Daniel - The beauty of WP is that there are a LOT of ways in which you can achieve the result. I wrote the plugin because I found the category-##.php system falls apart when you have multiple style templates and lots categories which you want to apply them to because you can only have one default category.php with the standard WP category logic. Take a basic use case of a designers site which has a portfolio, blog and tutorial section.

Without the plugin you’d have to duplicate a category-##.php file for every category even though you only have a few true templates. Even if you do a similar approach to the post and have a category-##.php that is nothing more than calling the true category template you’re still creating a LOT of extra unnecessary files. Or category mapping to template logic inside an uber-template that would be more practical to capture through the admin console.

With ICE, you create the 3 category templates and go to the manage > categories panel where you can assign them to the top-level categories. All nested categories will inherit them. They are named logically (categoryPortfolio, categoryBlog, etc) and you can style the singlePortfolio, singleBlog, etc accordingly. If you have particular design needs for a specific child category you can override the default for it through the manage > categories selection which has been customized.

Dec 30 2008 16:32

I have one question. been trying to use a different template for each category. but I have like 100 category ID.

any one have any idea?

thanks

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash