Different content for each category in WordPress
 

magic thumbnail

In this tutorial I’ll explain how to take your WordPress theme to a new level and detach it from the typical blog type template. By mastering this technique the possibilities are endless.


About

Using WordPress as a CMS is very “in” nowadays. I’ll be transferring all my websites to WordPress in the following months as it enables for hassle free maintenance and makes updating easy. You can do so much customising within WordPress, such as adding custom fields, displaying different styles (CSS) for each category, picking random posts, displaying custom content to search engine visitors, and some wackier stuff such as displaying all untagged posts, displaying different content depending on day, customising the dashboard and many, many more!

In this tutorial we’ll focus on displaying different content for each category.

 

First Method

For this tutorial we’ll assume that we have two categories that we want to customise: contact and news and that you are on WordPress 2.9 or newer (if you aren’t use the second method explained below).

First, go in your template folder and create two .php files called:

category-contact.php
category-news.php

By default, the template hierarchy within WordPress will select the first file it finds from the following list:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

So if you try to view the contact of news category now, it will show a blank page. That’s it basically! You can now, go inside each category .php file and add whatever you want inside; the default template will be bypassed.

What if you have a dozen or so categories, do you have to create a dozen of .php files with the categories names? No!

You just add the default content that you want to be displayed on most categories within the archive.php page and create only .php files for the categories you want to customise. Or, you can do it by using the second method explained below.

 

Second Method

The second method is to create two .php files for the two categories we want to edit, place them inside the theme and name them:

category_contact.php
category_news.php

Next, create another .php file and name it

category_other.php

This will be the alternative content to be displayed when the visitor selects neither the contact nor the news category.

Now, you go inside the archive.php page and delete everything that’s inside. Add the following code:

<?php

if (is_category(‘contact’)) {include (TEMPLATEPATH . ‘/category_contact.php’);
} elseif (is_category(‘news’)) {include (TEMPLATEPATH . ‘/category_news.php’);
} else {include (TEMPLATEPATH . ‘/category_other.php’);
} ?>

So we’re basically saying, if the category selected is contact, display the category_contact.php. If the category selected is news, display the category_news.php. If none of the ifs are true, then display the category_other.php. Simple and very efficient!

You can see an example of how I use this on amateur magician.

 

Conclusion

The same principle can be applied to the posts template. Say that you have a post where you want to display differently (a gallery page for example), you apply the idea from the second method (haven’t tried it with the first method yet).

Enjoy the method and good luck!

If you have any questions, pop them in the comments section below! Please subscribe and share it if you found the tutorial useful! Thanks!

References:
WordPress

Source Files are unavailable for this tutorial! No Source Files

Have anything to say? Add your comment below!
Comments
  • Sweet tutorial!… I have been looking for a method for handling custom category styling and could never wrap my brain around it because all the tutorials out there went into too much detail. Thanks for keeping it simple.

    25 May 2010 at 5:36 am
  • @Rob: You’re welcome! It was indeed a daunting task!
    I couldn’t get my head around it, so I decided to make an easier tutorial, the way I understood it!
    I’m glad you found it useful! As you can see, not many did (0 comments, besides yours) :D
    Let’s see what will happen with the 3.0 WordPress release!
    I’m waiting for that release in order to use it as the backbone on another website I’m working on (http://improvephotos.net), but they don’t seem to want to release it yet!
    Alex

    25 May 2010 at 1:53 pm
  • I’d really like to use this tutorial, but I don’t know where the template folder is supposed to be/go. Can someone help me out with the correct path in relation to my theme?

    Cheers,

    Chris

    5 June 2010 at 5:08 am
  • @Chris: the files should be in the /your-blog/wp-content/themes/name-of-theme/

    5 June 2010 at 10:23 pm
Add a comment