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. Hi !

    Great plugin. I was looking for it for a long time !
    It works fine exept that when it displays the main category, the name of that category is not linked any more. Before, there was a link on each category that let us click and see all the posts form that category. How could I fix it ?

    Thx a lot for your help !

  2. I just wanted to drop you some much needed praise *wink* Brilliant plugin it works perfectly, and is very very versitile I like that. Hopefully with all these requests for automation nothing will be lost. Keep up the good work Bennett and don’t let anyone take the fun out of it.

  3. Hi Bennett,

    Fantastic plugin! Thank you.
    Just one thing: the plugin only worked for me when i fixed on line 56:
    “$wpdb->get_row(…” to “$wpdb->get_var(…”, because it was returning a unusable Object. Don’t know if it’s particular to my instalation.

    I’m using this plugin for a myriad of functions and already integrated it to the Pages Form, to TOPcategorize my pages.
    Once again, thank you!

  4. Thanks for that, Alfredo. It looks as if you’re right — I don’t use that function with a post ID on my site, so I never encountered the problem. You must be a power user :). I will verify the problem and release an update. I have a couple of other tweaks I want to include too…

  5. Hi agin,

    No one can help me about my link issue ?

    thx

  6. Sorry about that, Flo! I will think about adding convenience functions to Top Cat to do this automatically. For now, instead of using

    topcat_the_main_category()

    which does not create a link, use this instead:

    echo '<a href="' . get_category_link(topcat_get_the_main_category_id()) . '">' . topcat_get_the_main_category() . '</a>'

    If you have any problems, email me a copy of your template file and I will try to make it work for you.

  7. It works perfectly ! Thanks a lot !

  8. I posted this in the WordPress forum, but then I came across this plugin. It’s not exactly what I was hoping for 😉 but maybe it can be adjusted with not too much trouble (???):

    I have to find a way to retrieve the top level category of a post.

    I have a few categories that function as top level category. They have no posts themselves. Beneath them are a few categories that contain posts. When I click one of them, for instance index.php?p=23 I want to know to what top level category this post belongs to, like this:

    Animals
    – Dogs
    — Golden Retriever
    — Husky
    — Rottweiler

    In this case Golder Retriever belongs to top category ‘Animals’.

    Any way I could still use this plugin?

    Thanks!

  9. hey Bennett.. i havent yet used this on my website but i will try it.. i have many posts which are on atleast 2 or 3 categories and it would be cool to highlight a main one. thanks

  10. Bennett,

    Thanks so much for this plugin. This is really something lacking in WordPress.

    Is there any way to make topcat return the main category’s slug as opposed to its name? I’m trying to make pretty URLs and would prefer not to rely on index.php calling on the category id.

    I tried to pick apart your code, but, alas, I’m not PHP wiz. I beleive Worpress knows the value as $cat_nicename, however. Any help would be appreciated.

    —Spencer

  11. Spencer, that’s a good idea. I’ll include it in the next update.

    Jack, I think that must be possible (though it’s not what Top Cat does). I’ll see if I can find out how to do it.

  12. I think we are all still breathlessly awaiting an update (says the man without PHP skills).

  13. Thanks for waiting, Spencer. I have recently updated the Evermore plugin, and I am working on Search Meter as well as Top Cat. Also my day job has gotten madly busy, I am living in temporary accommodation as my house is being renovated and my entire family seems to have come down with some kind of virus. I’m not playing for sympathy, but I just wanted to explain why things are rather slow…

  14. Take your time. Maybe I should learn my own PHP, huh?

  15. Bennett,

    Thanks again for all your hard work. The new slug feature is a godsend. It really pretties up links on my site. Any idea why the select the top category checkbox isn’t working in WP 2.1-alpha3? The plugin itself is working as all the old post I used topcat on still appear correctly. I just can’t select the topcat of new posts through the GUI or edit the topcat of old posts.

  16. Ah, so they finally broke it did they? I’ve just checked out the WP development trunk, so I will investigate at my pace (as they say in Japan).

  17. Yes, the category checkbox IDs have changed in WordPress 2.1, so Top Cat can’t find them in order to add the adjacent radio buttons. I’ll upgrade Top Cat to cope in a backwards-compatible manner.

  18. In the meantime, does it hurt to set this through PHPMyAdmin?

Leave a comment