Skip to content

Quick Tip: Using WordPress Slug-based Category Templates

Link To Full Story: Devlounge - Homepage Feed

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?

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*