14 Jan 2008

In WordPress sometimes you want a different look for different categories. It’s fairly easy to accomplish with a little coding and creating custom page templates.

Code for Page Template File

The only necessity for using a Page Template is the below code. Just place it at the top of the page template you are creating. A good point of reference to see how a Page Template is handled is by looking at the file ‘archives.php’ in your theme. It’s usually the second ‘Archives’ link in the theme editor for the default WordPress theme.

  1. < ?php
  2. /*
  3. Template Name: Custom Category
  4. */
  5. ?>

Code to Call Unique Templates

The below code should be placed at the top of ‘category.php’.

  1. < ?php
  2. $post = $wp_query- >post;
  3. if ( in_category('3') ) {
  4. include(TEMPLATEPATH . '/cat3.php’);
  5. } elseif ( in_category('4') ) {
  6. include(TEMPLATEPATH . '/cat4.php');
  7. } else {
  8. include(TEMPLATEPATH . '/cat.php');
  9. } ? >

As always you can find out more about creating themes and template tags at the WordPress codex.

4 Comments

  1. No. 1 David Airey 01/14/08

    I get so many new items for my ‘to-do’ list from you. Maybe I should unsubscribe. ;)

  2. No. 2 Jen 01/14/08

    Nah, just get a longer piece of paper. ;)

  3. No. 3 Brady 02/08/08

    Hello Jen, I just stumbled across your blog today and started digging through your archives and came across this post and figured I’d let you know that WordPress actually has a built in method for using category templates. It’s called Template Hierarchy, which means WordPress will actually search for very specific templates and gradually fall back to more general templates before it finally decides to use index.php.

    So instead of using Page Templates, you could create a category-3.php, category-4.php, and category.php and get rid of the code above. Check out Template Hierarchy and Category Templates in the WordPress Codex for more info. Keep up the great blogging!

  4. No. 4 Jen 02/08/08

    @ Brady- thanks for the tip. Anything to make my WP development easier, the happier I am.

Tell Me What You Think

About Me

My name’s Jen; I’m a cynical, sarcastic, ex-drummer who is fond of dark humor. I've held way too many factory & retail jobs but finally found my calling one Christmas holiday in a dark, musty basement. I am now a CSS & XHTML web standards looney and can be found daily—when I’m not at my Mac—at the local fair-trade coffee shop buying an iced-soy mocha no matter the temperature.

I am also the owner of Pop Stalin Design specializing in CSS & XHTML web design as well as custom WordPress themes.