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?






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.
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.
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!
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!
Want an avatar? Get a gravatar! • You can link to this comment
Thanks for this trick, I will use new function early.