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.

Compatibility

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.

Installation

  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

topcat_the_main_category_slug
topcat_the_main_category
topcat_the_main_category_id
topcat_get_the_main_category_slug
topcat_get_the_main_category
topcat_get_the_main_category_id

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.

Problems?

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.

Download

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
1542gqyprvQd7gwvtZZ4x25cPeGWVKg45x

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.

105 comments

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

  1. It should be no problem to set the main category directly — as you have probably figured out, it’s in the post_category column of the posts table. The fix is not quite as trivial as I thought, but I still plan to have it done when 2.1 is released.

  2. I’ve made a minor update to version 1.3, to make Top Cat work with the latest beta version of WordPress 2.1. There are still a few WordPress 2 wrinkles that I need to iron out and some new features I want to add, so version 1.4 will appear in the fullness of time.

  3. Hello!

    Quick question. Your ‘how to use tutorial’ perfectly works for all the Cats i have. But what slug should i use to set background for the Pages the same way?

    Thank you in advance.

  4. Paul, Pages don’t have categories, so I don’t think you can do it the same way. I’ve occasionally thought of writing a plugin to let pages have categories… perhaps one day.

  5. That would be the great feature – the same main slug for pages as well.

    Anyway thank you for your work!

  6. Great plug-in. What keeps me from using it right now is that I have about 110 posts to go through. Is there some way of making this more easy?
    I.e. One page that displays all post headings, along with the options of assigning the different categories? Rather than having to click through each single post…

    Thanks you ever so much!

  7. There was a bug in the topcat_the_main_category_id() tag that prevented it from doing anything at all. Fixed and now at version 1.4.

    And Modifoo, your idea is a keeper. I would like to implement it, but here’s a question: what’s even more time-consuming than having a baby? Answer: having another baby.

  8. Bennet, thanks for your great plug-in, I’ve found it very useful and am looking to use it in permalink structures (perhaps what Mark Matienzo was asking.)

    For example, /%topcat%/%postname%

    Is there a way to do that? If not, have you any suggestions for someone with limited PHP skills to start on a hack—I looked at rewrite.php and it made sense but didn’t seem amendable to amateur tweaking.

  9. Hi! It’s a really useful plugin, thank you!
    But I have one problem (after upgrading to WordPress 2.1) – the_permalink of the post don’t know anything about my main category. for example, if my post belongs to two categories (first with id=5 and second, id=10) and the main category was created later then not main (and has bigger id), the permalink would be first/post_slug, but I need it would be second/post_slug
    Earlier, in WordPress 2.0 it worked without this mistake.
    Hope, you can advise something.
    And sorry for my English.

  10. Hi,
    thanks for the great plugin, I just don’t see the radio button near the categories. Using WP 2.0.10

    Any ideas why? Or how to fix that? thx

  11. Yantar, I considered adding this feature but didn’t have time – it seemed a bit tricky to do. If it worked for you before then it was probably just chance… it’s still on my to-do list though.

    Vlado, I will try to find time to check this out. I’ve not seen this problem, though I have not tried that exact version of WP.

  12. This is not only a great plugin, but also a really good idea! I love the fact that it does not need to modify the database structure and how you took advantage of a feature that the WP developers decided to drop.

    There’s only two things I see that could make it even better:

    1. What some guys here were already asking (namely Mark A. Matienzo, Yuri Gadow and Yantar): having the main category slug in the permalinks. That would be really useful.

    Unfortunately I don’t know a word about coding (I tried), but May I ask you to have a look at the sCategory plugin? That’s exactly what we are talking about and the script is similar to Top Cat in some ways. Sorry if you already knew that or if my suggestion is totally useless.

    2. It would also be really nice if the JavaScript requirement could be skipped. I would imagine it’s quite hard to change the categories form without editing any core files, but PozHonks wrote some code to change the form at the same point Top Cat does include its bits with JavaScript (the replacement looks simpler, though) and Otto42 mentions that there’s two hooks to modify the post writing page.

    Again, sorry if those suggestions sound too stupid. As I can’t code, I just wanted to see if I could help by doing some research.

    BTW, for all those being a bit rude to developers, please think twice before doing it again. Guys like Bennett could just keep their work to themselves and the share it with the comunity instead. If you think you deserve some respect for what you do at work, you should show some to those who try to help for the heck of it.

    All the best.

  13. Thanks for all that, Frames. Yes, the sCategory plugin will be useful to provide inspiration and ideas for the permalink feature. As for the JavaScript requirement, I think the PozHonks way (using output buffering) is the only way to go. I don’t normally like using OB because of efficiency — I was hoping there would be another way to do it — but as this is an admin-only page is shouldn’t matter too much.

    I will make the changes probably in the next update to Top Cat, but I don’t know when I will be able to do that.

    And thanks for your other comments. I think Top Cat is a bit less obviously useful than some of my other plugins, so I understand why some people don’t like it.

  14. It’s cool to hear my two cents are worth it. Thank you for your interest, Bennett.

    Let me just point you out to another resource that I’ve found: add_rewrite_tag(). Internal permalink control and the documentation for it are in their infancy, but it looks to me as if this is what we are looking for. At least, with that %postname% mentioned in the page. Hope that helps.

Leave a comment