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.
Share this page:
  • Twitter
  • Digg
  • Slashdot
  • del.icio.us
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Reddit
  • Facebook
  • Print

99 comments

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

  1. Peter, I suppose this could be because Top Cat is not internationalised. I haven’t had time to see how Language Switcher works its magic so I can’t be sure. Can you tell me if the WordPress built-in function get_the_category() works with Language Switcher? If that works and topcat_get_the_main_category() does not, then that will give me something to go on.

  2. Hi Bennett…

    sorry… I missed your reply.

    New problem: Are you going to be able to make this WP 2.3 compatible… I fear it may not be possible!

    Thanks
    Peter

  3. It would certainly be possible to be 2.3 compatible, but it would involve a fair bit of behind-the-scenes changes. I am considering it, though for my own purposes I plan to use the new tagging feature instead of Top Cat.

  4. Bennett!

    Not sure why, but TopCat IS working with 2.3!!!
    Just can’t figure out how to implement the tags feature in 2.3!?!
    How does one enter tags?
    Peter

  5. I’ve been looking for a plugin that will allow me to do what i need to accomplish.. i want to categorize posts in a hierarchy (i.e. Teaching Resources -> Assessment -> Rubrics), but I also need to categorize posts by TYPE of resource (web link, book, etc ) .. if I use the topcat plugin and set the TYPE as the main category then I can show all books with one template and all web links as another template, etc. .

    MY QUESTION.. is can i group the topcat’s so that all books will display first, then all weblinks, etc.. or will they be jumbled ?

    Many Thanks ..

    Adon

  6. Got a problem updating posts with WP 2.5: if I try to change the top category I get a 500 internal error. Any ideas?

  7. Domiziano, I suspect that Top Cat probably does not work with WP 2.5. With WP 2.5 tags, Top Cat is not that useful anyway.

    If I had time I would upgrade Top Cat to work with WP 2.5. I don’t know whether I ever will though…

  8. Thank you for the answer Bennett. :-)

    Anyway I still think Top Cat is an useful plugin, at least the way I’m using it. I’ve mashed it with the Image Extractor that extracts the first image from a post and displays it as a thumbnail. When a post doesn’t have an image it shows a preset image according to its top category. :-)

  9. Hey I’m trying to use top cat to work as the byline for the single post section of my site and I’m having trouble getting it to display right here is my code: The theme is The Morning After and the problematic site is here: http://justafan.tandrew.com/?p=20

    By <a href=”">topcat_the_main_category; ?> ⋅ ⋅ ⋅ ⋅ Post a comment

    Thanks!

  10. Hi Bennett,

    great plugin, but would be great if there was an update for WP 2.5 also

    anyway great work

  11. Bennett, did you implement the permalink change re: comment dated 19 August 2007?

    I’ve installed on 2.7 and it doesn’t do anything to permalinks. A bug?

  12. Ciao Bennet. This is just a great plugin! I need it but it doesn’t work on 2.7… hope you’ll upgrade it sooner or later ;)

    bye form italy

  13. ps: is there a plugin that do similar thing?…

  14. Bennett!
    I have used this plugin in older versions of WP, and would REALLY value an upgrade. It really is a very useful plugin, and now limits me from upgrading WP on an older blog.

    I often have blogs with multiple categories and need to be able to specify a Top Cat.

    Please consider upgrading to 2.7x

    thanks
    Peter

  15. Can I use this plugin for wordpressmu

  16. @Aeon, I haven’t tested Top Cat on WordPress MU. It’s worth a try, but they may not be compatible.

  17. It looks pretty interesting.Im going to give it a try

  18. im going to give it a try, cause i am so intrigue if that’s gonna help me for creating for my blog.

  19. I used Top Cat quite often in past blogs I have created. But the radio buttons are not showing in my new blog…I am running WP v2.9.2.

    Any chance you will be upgrading Top Cat so it is compatible with newer versions of WP? Would be eternally grateful :)

  20. I won’t be upgrading Top Cat myself. It is open source, so if any other developer wants to take it over, feel free.

Leave a comment