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. This looks really useful – but it doesn’t quite work for me. In both Firefox and IE6, I get the same result, which you can see in this post on my site.

  2. It’s a great plugin and it works well. Thanks! I’d suggest either to have a readme file or to put more instructions into the file. Even on this page you have only Have a look inside the plugin source file for more details. – which is not very helpful for people who are “code-challenged”, as myself 🙂
    Also, how can I make the top_cat a link?

  3. Good point Moshu — you do need to edit your templates and know a bit of CSS to take full advantage of Top Cat. I’ll see if I can put together a little tutorial. I will also investigate automatic main category styles. So many ideas, so little time…

    I’m not sure I understand your last comment about making top_cat a link. Can you give me an example?

  4. I should also mention that the problem Les had is fixed now — I updated the plugin a few days ago.

  5. Hello,
    I’m trying to fix up my wordpress using plugins.
    I’ll try Top Cat first and let you know how it works.
    Also I use seach engine tags for better PR
    Thanks for the info
    John M

  6. You know what would be great, a readme showing how this plug-in works! Like how can I change the colors of titles using the tags. I mean comon even a simple example would help a ton, not user freindly at all.

  7. Sorry about that. I should make it more clear that you need to be comfortable editing your WordPress theme and using HTML and CSS. If you can do these things, then it should be fairly obvious how to make Top Cat work. If not, then you will have to wait for a future version of Top Cat that automates such things. This future version is only in the planning stages so far.

  8. I am comfortable editing wordpress, which has nothing to do with the awful documentation that goes with this plug in, why post it if you don’t want people to use it.

  9. It’s certainly not the case that I don’t want people to use it! Clearly, Top Cst is not for you. Sorry about that — I’m sure there are many other plugins that you’ll be happier with. Good luck!

  10. Hi Bennett,

    I installed topcat in my website and everything seems to work but it doesn’t quite what iam afther but i think somewhere it should be possible

    I want certain images next to my posts in wordpress. Therefore i use this code:.png” alt=”Category: ” />

    But when i select more images at the same time, it always falls back to the maincategory. I thought with Top Cat the category id could be changed to the main one so that the script can load the best image.

    Could you please help me out?

  11. Say can you take some time or get your girl friend or family member to draw up a READ ME and also include some examples WITH full display. When you guys toss out these plug ins and only include a show of variables you leave many people grasping at air that don’t yet fully understand the css and php brackets and characters or where exactly they belong. I always work myself off from pre-made samples, thata way i didnt always have to needle the guy for help all the time on just minor stuff thats elementary to people like you.

    Thanks

  12. Joseph, sorry I have not had time to provide examples. I really need to think about a nice way of automating the plugin so you don’t have to edit CSS and theme files to make it useful. WordPress 2.0 has better integration between themes and plugins, so maybe I will be able to do something with that.

    But just now I don’t have time to do anything on it at all. I don’t even have time to write this comment, so…

  13. I’ve updated Top Cat based on the changes in my post on Setting the “name” attribute in Internet Explorer. It should now finally work correctly in Opera and all other modern browsers.

  14. Can the top cat plugin also be used to force me to select the appropriate category before I publish my post? I understand the ‘uncategorized’ category is needed, but why not disable the publish button before one of the categories are selected. This prevents posts are not assigned to categories and forces me to think to select the category before hitting the publish button. 😛

  15. Frans, that’s a great idea for a new plugin! I’ll do it if I have a bit of time…

  16. Bennett – If possible, I’d like to use Top Cat to specify the primary category for the permalink. Is this possible?

  17. Mark, I don’t quite know what you mean. Top Cat assigns main categories to posts, whether viewed on the home page, an archive page, or on their own page via a permalink. Do you mean something else?

    Frans, I am testing an update that automatically assigns the main category if you forget to click one. That’s a Top Cat specific version of your idea. I’ll update this page with the new code soon.

Leave a comment