WordPress: How to display categories in hierarchical order

[Total: 2    Average: 3.5/5]

The most recent versions of WordPress include the possibility to organize taxonomies in hierarchical order. Categories are by default hierarchical taxonomies, while Tags are not (they are non-hierarchical or multi-faceted).

When WordPress displays such categories with the post, by default sorts them in alphabetical order. However it would be more logical to have them arranged in a hierarchical way, which is exactly how they were defined. This post shows how to do it.

To make things clearer, let’s work on a real-life example.

If your blog contains a review of your favorite restaurants around the country, you may create a hierarchy of categories to indicate in which zone, state and city a restaurant is. In this way you can promptly list all restaurants that are in the same city or state or area by selecting the corresponding category.

For example, La Campania is a very good Italian restaurant in Boston, MA. Your review will then be marked with the hierarchical categories East Coast –> MA –> Boston.

When your review is displayed, you would like the categories to appear exactly in hierarchical order and not sorted in alphabetical order and separated by a comma like it is by default in most of the themes.

So instead of showing:

Boston, East Coast, MA

you would like to show:

East Coast > MA > Boston

Where each tag is also a link to the respective category, so to be able to click on a category and show all restaurants that are also classified in the same zone, state or city.

Here is the code that makes it happen. You should replace the default one in single.php, archive.php and any other template that displays posts.

How it works: Basically we are retrieving the list of categories for the current post and then using the first one [0] to retrieve all parent categories through a call to get_category_parents(). This function will return the categories for the current post in hierarchical order, while the more widely used get_the_category_list() returns categories sorted alphabetically or by ID.

The categories will be links (effect of the TRUE parameter), so it is possible to click on them and list all posts with the same category.

We also append the title of the post as last unlinked category and add the post date in front for completeness.

Hope this helps. Happy blogging!

You may also like

4 Comments

  1. Hi! First of all, thank you for this post, it is exactly what I was looking for.
    I have just a little question : I would like to remove the title of the post. I could remove the post title, but the last “>” remains. Do you know how can I remove it?
    Thank you!

    1. The trailing ” > ” is generated by get_category_parents. You can simply remove the last 3 characters from the $categories string before printf. Use the modified code below:

  2. This article is old, but THANK YOU for this. I have looked for nearly 2 days for a code that does exactly this (clients can be quite picky!). Great short piece of code. Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.