Feature
Post

Category
Code


Quick Tip: Using WordPress Slug-based Category Templates

If you create custom themes for WordPress, chances are you’ve already used category templates in the past. They usually look like this:

category-x.php

where x corresponds to the ID number of the category, and are extremely useful if you want to give a specific category a certain look. For example, if you wanted to add an “Asides” area to your theme, you’d create a category named Asides, get the numerical ID of that category (e.g. “3″) and create a template file named category-3.php

My problem, of course, was having to find the ID of the category, which could vary from one WP installation to another. The truth is that I usually chose to go the route of conditional queries instead, doing something like this:

if ( is_category(‘asides’) ) {
// what I’d like to show for my asides
}

WordPress 2.9, however, has added support for slug-based category templates. So instead of naming my template category-3.php, I can go ahead and name it something wonderfully semantic like category-asides.php

The hierarchy in which WordPress looks for which template to use has been updated as well. Now, it’ll look for the slug-based template first, then the ID-based template, and finally the default template.

Do you use WordPress category templates in your themes?


  1. By Stopped by posted on January 11, 2010 at 6:33 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I use Asideshop for asides and similar cases when I need to style posts in a particular category… differently. But thanks for the tip. Taken notice of.

  2. By Michael Fields posted on January 13, 2010 at 5:42 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Lorraine, Great article for a great advancement in WordPress theming. I have been using slug-based category templates since they have been introduced in 2.9 because they allow me to develop locally much easier.

  3. By Cam posted on January 17, 2010 at 3:08 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    DEFINITELY! I had a project I developed last summer w/ category-x.php files. it stalled literally until this week! So now i am using the category slugs. Thanks for the help!

  4. By Bobby Marko posted on January 22, 2010 at 3:54 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I’m using category slug templates on a current project but I’ve run into an issue where the slug template doesn’t get used for sub categories. For example category-events.php gets applied to http://siteurl.com/categories/events but not http://siteurl.com/categories/events/music .

    Anyone encountered this issue? I’d hate to have to create a template file for each and every sub category!

  5. By Ivan posted on January 27, 2010 at 12:08 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Thanks for this trick, I will use new function early.

  6. Trackback  Quick Tip: Using Wordpress Slug-based Category Templates | Devlounge by กิ๊กก๊อก : ตามหาฝันบนโลกออนไลน์ !!!A Quick Guide to GPS Navigation Systems | #1# GPS Navigation System BluetoothWebmaster Crap » Blog Archive » Quick Tip: Using Wordpress Slug-based Category Templates | Devlounge