how to get the categories for a single post in a hierarchical way

The question is simple.
Imagine that I have a post named ‘Single Post’ which is inside the category ‘sub-cat’ which is a child of ‘parent-cat’ which is a child of ‘super-cat’.
What I need to do is to display all the related categories within the post page in the following order:

super-cat > parent-cat > sub-cat > Single Post

5 Answers
5

<?php 
    the_category( ' > ', 'multiple', $post->ID); 
    echo ' > ';
    the_title();
?> 

This works correctly when the post is in just one category. But if it’s in multiple categories, or if the category parents are also selected — in your case, if the post is also in super-cat and parent-cat — then it displays those categories twice.
So this is probably not going to do it for you.

I suspect some of the breadcrumb type plugins might have solved this though.

Leave a Comment