Making Unrelated WordPress Posts Look Like They Are In The Same Category

In my page header you’ll see there’s the linked photo for “Japan and Other Travel.” I recently decided to separate my “Japan” category from my “Travel” category, since I’ve been writing a fair number of posts about Japan that aren’t directly related to my travel there. But I still have plenty of posts that are about my time in Japan. So supporting that link in my header became a problem since WordPress doesn’t have the concept of multiple category parents (that is, WordPress can only display posts together on a page if they are in the same category, or have a common category parent).

One solution would be to use tags – to give all my “Travel” and “Japan” posts a common tag, and then link to the tag. That’s a perfectly good solution, but I decided against it because I’m lazy. I’m not currently using tags at all, and I don’t want to go back and update something like 150 posts with the new tag. Even if I wrote a database query to take care of the old posts in a single batch, I’d still have to always remember to put any new posts under the right category and the right tag. In addition to being lazy, I’m also forgetful.

Instead, I did the following:

  1. I created a new top-level category named “Japan and Other Travel,” and I made a note of the category ID number assigned to it. This category will be an empty shell. I won’t ever assign any posts to it.
  2. I created a new template in my theme named category-96.php. That number in the filename is the ID number that was assigned to the category. When displaying a category page, WordPress will automatically first look for a template specific to that category before moving on to a catch-all template.
  3. category-96.php consists of nothing more than the following lines of code:
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    require_once(TEMPLATEPATH . '/index.php');

    This uses some of the same code that’s in a post I wrote last year, about how to limit the categories that appear on your home page. Normally this category page would have nothing on it, since I don’t have any posts assigned to it. The call to query_posts() overrides that, and instead gets posts from the two category ID numbers that I passed to it. The $paged variable keeps track of which page someone is on if they click links to see older entries in the category. The require_once() call pulls in the index.php file for my theme to display the posts (if my theme had a category.php file, I would have used that instead).

And that’s it! The empty category doesn’t show up in my category list in the sidebar, because by default wp_list_categories() excludes categories with no posts in them.

I like this solution because now that I’ve set it up, I can go back to being lazy and forgetful.

One Comment

  1. Reply
    Krissi March 18, 2009

    Great post! I’ve used it on my site, and it works perfectly. 🙂

Leave a Reply