Top Cat: a WordPress plugin

Latest version is 1.4, released on 10 March 2007.

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 by clicking here. Thank you.

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 — Change the (more...) link so it jumps to the full post, not just the part after the link.
  • 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.
  • Top Cat — Specify a main category for your posts, and use template tags to display posts differently according to their main category.
Share This | Comments | Permalink | Trackback | Comments feed

85 responses

Other comment pages: « 1 2 3 4 [5]

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

    Thanks

    Vic

    Vic | 3 August 2007
  • Thanks for such a great list of plugins, this list are so helpful for the new guy.

    Mike | 5 August 2007
  • 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!

    Nick Georgakis | 18 August 2007
  • [...] Top Cat allows you to specify a main category for your posts. [...]
  • [...] Top Cat allows you to specify a main category for your posts. [...]
  • 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.

    Bennett | 19 August 2007
  • 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

    Peter Lurie | 31 August 2007
  • 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

    Peter Lurie | 31 August 2007
  • 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.

    Bennett | 31 August 2007
  • 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

    Peter Lurie | 25 September 2007
  • 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.

    Bennett | 26 September 2007
  • 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

    Peter Lurie | 26 September 2007
  • Trackback | smr’s blog » beer | 1 October 2007
    [...] me. - categories list. - a few plugins. Will they work ? Esp FAlbum, Page Links To, PHP Exec, Top Cat The current version is good enough. I can [...]
  • [...] Top Cat allows you to specify a main category for your posts. [...]
  • 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

    ad0n | 16 November 2007
  • [...] Top Cat: a WordPress plugin » Semicolon (tags: blog plugins category plugin wordpress) [...]
  • 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?

    Domiziano Galia | 14 April 2008
  • 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…

    Bennett | 14 April 2008
  • 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. :-)

    Domiziano Galia | 16 April 2008
  • hi. Thanks for plugin

    perfect.

    regards

    baron | 14 May 2008

Other comment pages: « 1 2 3 4 [5]

Post a comment

(required)
(required)
Close
E-mail It