Top Cat: a WordPress plugin

Sunday, 22 May 2005

Latest version is 1.4, released on 10 March 2007.

Note (25 May 2009): WordPress 2.8 will be released in a week or so, and will contain changes that are incompatible with Top Cat. Therefore I am officially discontinuing support for Top Cat. It will still work for previous version of WordPress.

Note for developers: The relevant WordPress change is the removal of the post_category column from the posts table. It would be possible to reimplement Top Cat using a custom taxonomy, but this would require more time than I have right now!

Top Cat is a WordPress plugin that allows you to specify a main category for your posts. Even though WordPress posts can have more than one category, you may want to specify which of them is the main category for each post. Top Cat lets you do this and also provides template tags for displaying the main category.

Using style sheets, you can then apply different styling to posts based on their main category, even if they belong to the same set of categories. For an example, see my Bennettarium website.


Top Cat has been tested successfully with WordPress versions from 1.5 to 2.1, and should also work with newer versions as they are released. The administration functions may not work on some older browsers or where JavaScript is disabled.


  1. Download the topcat.php file (see the end of this article for download location)
  2. Copy topcat.php into your WordPress plugins directory (wp-content/plugins).
  3. Log in to WordPress Admin. Go to the Plugins page and click Activate for Top Cat.

How to use

When you create or edit a post, you will see round radio buttons next to each “Category” check box. Check the checkboxes as normal, and also select the radio button next to the category that you want to specify as the main one.

In your theme pages, you can use the template tags defined in the plugin to get or display the main category’s name or ID. The tags are


These tags can be called within The Loop, or outside of The Loop if you give them the ID of a post. Have a look inside the plugin source file for more details.

Example usage

To style posts according to their main category, you’ll need to edit your theme. For example, you could edit the default WordPress theme by adding the main category name to the class for each post:

<?php while (have_posts()) : the_post(); ?>
  <div class="post category-<?php topcat_the_main_category_slug() ?>"
    id="post-<?php the_ID(); ?>">

This gives each post the CSS class “category-categoryslug“. For example, if you have a post with main category “New Zealand”, then the post will have the CSS class “category-new-zealand”. Then you could edit the CSS file for the theme to vary posts’ background colour depending on their main category:

.category-new-zealand { background-color: #cfc; }
.category-australia { background-color: #ffc; }

Your posts with “New Zealand” as their main category will have a relaxing green background.


If you find any problems, please let me know here by leaving a comment at the bottom of the page.

Infrequently Asked Questions

What is this good for?
Good question. Suppose you have a post about the Cannes Film Festival, and another about a French film. Both might be in the “France” and “Films” categories, but perhaps you’d want to put the film post mainly in the “Films” category and the Cannes post in the “France” category.

Using Top Cat’s template tags, you can then style the posts differently based on their main categories. For example, see the different colours on the posts on my Bennettarium website.

I don’t see the radio buttons on the admin screen!
The administration functions of Top Cat work on all modern web browsers. They probably won’t work on very old browsers such as Netscape 4.

How did you add the radio buttons to the admin screen?
Have a look at the code… It’s slightly tricky because some browsers work differently to the way you might expect, or at least different from the way I expected. See Setting the “name” attribute in Internet Explorer for more details.


You can download topcat.php or view the source code. Don’t forget to check out all the other plugins available here — there’s bound to be one that you will find useful.

I write these WordPress plugins because I enjoy doing it, but it does take up a lot of my time. If you think this plugin is useful, please consider donating some appropriate amount.

Click here to donate using a credit card or PayPal.

Send Bitcoins to address

Full WordPress plugin list

  • Code Markup — Quickly paste code samples into your posts -- you can even include HTML markup in the code sample.
  • Evermore — Automatically display a short preview of your posts on the home page and other multiple-post pages, along with a link to the full post.
  • FixBack — Ensure trackbacks and pingbacks are sent with the correct link back to your blog.
  • Less — Less is no more. It has been renamed to Seemore and moved to its own Seemore plugin page.
  • Plaintext — Allow your readers to download source files (e.g. PHP, HTML, ASP) as plain text.
  • Safe Title — Use HTML in post titles in the default WordPress theme (or any other theme).
  • Search Meter — Find out what people are searching for on your blog, so you can write what your visitors want to read.
  • Seemore — Change the (more...) link so it jumps to the full post, not just the part after the link.
  • Top Cat — Specify a main category for your posts, and use template tags to display posts differently according to their main category.


You can leave a comment, or trackback from your own site.

  1. very good put up, i actually love this website, keep on it

  2. I have recently started a site, the information you offer on this website has helped me greatly. Thanks for all of your time & work.

  3. It’s a pity you don’t have a donate button! I’d without a doubt donate to this excellent blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this blog with my Facebook group. Chat soon!

Leave a comment