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.
Be Sociable, Share!

101 comments

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

  1. Regarding the usefulness of Top Cat, if you are able to incorporate the main category (or the Top Cat to be correct :-) to the permalink, it would be more beneficial than what we all could think of at first sight. At least with another neat and also highly requested plugin, the Top Level Plugin. That would make permalinks very flexible.

    To put it straight, Top Level is quite simple, it just removes the “category_base” in the archives permalinks, something that is being longly requested in the forums.

    So if one just puts together Top Cat + Top Level, adds /%top_cat%/%postname%/ (or the like) in the custom permalink structure, and leaves the base category blank in the Permalinks admin section, he’ll have the killer permalink structure. And something that’s not truly possible with the current WP code.

    Let me set an example to make things clear. With WordPress you can have something like this out of the box:

    http://server/topics/code/wordpress/

    … where server is your domain name (a fixed string), topics is the category base (again, static) and code/wordpress is the category itself (the only part that’s “dynamic” in the url).

    Looks cool at first sight, but unfortunately (a) topics are the same for every post in the database and [doh!] (b) if you have more than one category in the post, the category in the permalink is “chosen” by WP, not the user [double doh!].

    If we use Top Level, we can have just:

    http://server/code/wordpress

    … which solves item “a” above. If you include all the other categories one author can have, this is great, as you have a very well structured site by only using categories. Just add some pages for the more static content if you want. I don’t think you can have a page with the same slug than a category (not sure, I’ll test it), but that’s not a problem, really. And this is about choice, anyway, just avoid pages with same slug than any top-level category (second-level categories should not be a problem here).

    Now, with (the “improved”) Top Cat, you could get rid of problem “b” and have exactly the same url:

    http://server/code/wordpress/

    The BIG advantage here is again that the blogger/user/author is deciding which category really matters for him/her. That’s what you did with Top Cat in the first place, of course. If the Top Category is the main “section” of the site, it looks like a perfect option.

    To me, this helps a lot when using WP both as a CMS or as a “multiple-blog” tool. And this would help a lot of people separate their site in several “sections” or sub-weblogs with little or no effort. Think about this:

    http://server/journal/

    http://server/photolog/

    http://server/articles/

    http://server/reviews/

    … and so on.

    Just add a link to each category and you have a menu for the site. Obviously, you can style the posts in different ways with the Top Cat and CSS. Cool, straightforward and clean, isn’t it?

    You could have the same permalink structures without Top Cat provided that you assign one (and only one) category to each post. But if you have a category for tags, that’s a no-no. Same if you have a category for authors or whatever you can think of.

    Of course, there would always be the option to use the top category in any other part of the url. Or don’t do it at all.

    My personal opinion is that these two features (having the option to turn “base_category” off and deciding which is the main category) need to be in WP’s core. But that’s only me, you know. To my knowledge, TextPattern can do something similar to this but does not have the flexibility of multiple categories, and b2evo has the main cat feature but does not know how to incorporate it in the url. I’m not sure about MovableType and the rest. I think WP developers are thinking on giving the users the ability to get rid of the category base. That would make Top Level redundant, but Top Cat would still be useful. I don’t think something like Top Level has to be in Top Cat’s code, anyway.

    Excuse me for the looong rant here. I’m sure it can be explained with less words and stuff.
    :-)

  2. Frames, you say you don’t know coding but you are a software designer at heart. You’re right that Top Cat would be more useful if it had the permalink feature — the only reason I have not implemented it is that I don’t need it personally. (Lazy, I know.) But I do plan to implement it. However…

    When WP 2.3 comes out, it will have support for tagging in the core. When that happens I plan to switch my personal blog to allow only a single category per post, and use tags for other “categories”. If you use tags like this, Top Cat is not really necessary. We just need a plugin to enforce single category selection — the PozHonks code does that.

    As I say, I still do plan to do the permalinks feature in Top Cat. Only then will I consider it “finished”. Thanks for your support and encouragement!

  3. Hey, very good plugin. It’s very usefull for me. Great. Martino :)

  4. Frames: Try this instead of the_permalink()

    /post_name; ?>

  5. Frames: my comment got croped here is the php code for permalink:

    echo topcat_the_main_category_slug().’/’ . $post->post_name;

  6. Thanks for such a great list of plugins, this list are so helpful for the new guy.

    Thanks

    Vic

  7. Thanks for such a great list of plugins, this list are so helpful for the new guy.

  8. Hello Bennet,
    After reading the comments on this page ,especially Frame’s ones, and wanting to test if they can be coded in PHP I have written a similar plugin NG Primary Category WordPress pluginwhich inserts the radio boxes without using any Javascript (through PHP output buffering & regular expressions) and requires no permalink structure modifications like sCategory.
    You might want to check the source code.
    Your suggestions ,coming form a seasoned PHP programmer, are welcomed since my PHP experience is limited.
    I am using your Search Meter plugin in my site and really love it!

  9. Nick, your plugin looks great. It’s definitely better to avoid the JavaScript dependency. As for the permalink feature, it is Top Cat’s most requested feature but it’s tricky to do it without proper WordPress hooks (as you have discovered!). I’ve finally figured out a simple way to do it so the feature will be in the next release of Top Cat (this month or maybe next).

    Anyone looking to use Top Cat should consider Nick’s NG Primary Category plugin too.

  10. Hi!

    I was thrilled to find this Plugin, which fills an important function on a blog I am working on.

    However I have come up against a conflict/challenge.

    I am using “Language Switcher” (http://www.poplarware.com/languageplugin.html)
    which requires one to wrap the respective languages in Tags. Post/Page content is wrapped using the following tags:

    for example: Katze Cat

    Categories require different tag enclosures:
    for example: [lang_de]Katze[/lang_de] [lang_en]Cat[/lang_en]

    The Category enclosures work for all category titles, EXCEPT when calling the top_cat function… instead of either Cat or Katze appearing, both words plus enclosing tags are visible.

    I am using the following call:

    Can you suggest another way I can get the Language Switcher to work, when calling the topcat function?

    Thanks
    Peter

  11. The function I use didn’t display:

    php echo topcat_get_the_main_category()

    I have removed the ‘hairpins and question marks… hopefully this will appear now.
    Peter

  12. 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.

  13. 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

  14. 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.

  15. 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

  16. 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

Leave a comment