Jepson Center for the Arts

Get Unique Categories in Wordpress

I currently categorize posts under a few main (Parent) categories and then specific (Child) categories per post like so:

  • Code
    • CSS
    • PHP
    • JavaScript
  • Business
    • Sales
    • Marketing
    • Human Resources

I’ve been looking (although I’ve only been looking for about a week) for a way to list unique categories per post. The default "the_category()" template tag lists all categories a post falls under, allowing the user to specify the separator and the parent/child relationship, but I didn’t want the redundant Parent and Child link(s) listed.

So if a post is categorized as Code>PHP, the "the_category()" template tag will return a list with links to the Code Category (parent) and PHP Category (child). Since “PHP” sits within “Code”, listing both is redundant for my purposes. Instead, I wanted just “PHP” to appear.

In my never‐ending quest to make script as user‐friendly as possible, I also wanted a link to the Parent Category to appear if a Child Category wasn’t selected when creating the post, and vice-versa; forgetting to select a Parent Category (which in my opinion, should automatically happen when selecting a Child Category — hint, hint WP devs).

The result is my first plugin written for Wordpress. It’s pretty straight-forward; It generates a HTML string of the categories associated with the current post. The name of each category will be linked to the relevant category page. If there is a redundant parent/child category pair, it returns a string of just the child categories. If there are no child categories it returns a string of parent categories.

Installation

  1. Download the "mish_wp_unique_cats.zip" folder, then extract.
  2. Upload uncompressed folder to your plugin directory (wp-content/plugins).
  3. Activate the plugin through your Admin Dashboard.

Parameters

The plugin accepts 2 parameters; the separator and the ID of the post to use. If you set the post ID, you have to set the separator first, even if you want to use the default separator. See examples below.

Usage

The default usage is to separate the links with a comma and a space (", ") and to pull category links for the current post.

To call within your template, copy and paste the following code wherever you’d like it to appear (typically within the post‐meta paragraph).

Example 1

Using the default.

<?php mish_wp_list_unique_categories(); ?>

This will return something that looks like: "CSS, HTML, JavaScript".

Example 2

Using a custom separator.

<?php mish_wp_list_unique_categories('&#124; '); ?>

This will return something that looks like: "CSS | HTML | JavaScript".

Example 3

Using a custom post ID.

<?php mish_wp_list_unique_categories('', 29); ?>

This will return something that looks like: "CSS, HTML, JavaScript", where “29” represents the post id you’re calling and keeping the separator at the default.

Conclusion

I hope this comes in handy for someone when developing a theme or just for learning purposes. It was designed specifically to handle the categorization used on this site, but it can be expanded to handle more parameters and fundamentally it could be used in other applications. I’ve tested it in some scenarios, but not many. Whenever I develop a script, I intentionally try to break it, so I can spot areas of improvement. If you see any, feel free to let me know!

Leave a Comment

HTML is disabled in comments except for comment-author URLs. Additionally, comment moderation is enabled, meaning, if you don't see your comment either it’s your first time commenting, you’re spam, or there was a glitch in the filtering. If it’s two of the three (guess which) no worries, it’ll show up soon. However, this site reserves full right to remove any comment at its discretion.

If you want a commenter avatar, you can learn more about them here.

Privacy Notice:

No personal information is collected on this website. We also do not collect eMail addresses or telephone numbers of people who contact us directly. We will only use them to respond to your questions or comments.

Please complete the required fields: Name, Mail, Comments, Validation .

Click to close.

Snapshot: Post

Posted: December 2nd, 2008

A Wordpress plugin for getting unique category links for your posts. How to prevent redundant category linking when Parent/Child categories are present.

Disclaimer: While this script may come in handy for others, I designed it specifically for use on this site. I haven’t tested it in any version of Wordpress other than those that I’ve used (v2.6.3–v2.8.6). Feel free to use it if you’d like, but I make no guarantee it will work with your current theme or Wordpress setup. For a complete Copyright Notice and Disclaimer, visit the About Section of this site.

Topics

Or you can check out the full archive here: Article Archive.

Search