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

    the_category( ' > ', 'multiple', $post->ID); 
    echo ' > ';

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.

  • In case there are several categories per post, you need to find the "final" ones (sub-cat's) and then get their parents one by one.
    – uncovery
    Jul 22, 2019 at 12:24

you need to sort the categories first.

// get categories of post in sorted order
$categories = sortCategories(get_the_category());     

foreach($categories AS $category) {
    echo $category->name; // Plot category name
echo get_the_title(); // Plot post name

function sortCategories($categories) { // Sorting the category
    usort($categories, "cmpCategories");
    return $categories;

function cmpCategories($category_1,$category_2) { // Sort function
    foreach(get_categories(array("parent" => $category_1->cat_ID)) AS $sub) {
        if($category_2->cat_ID == $sub->cat_ID) return -1;
    return 1;

I hope this helped.


This should do the trick:

$cats = get_the_category(); //retrieve cats for post

foreach ($cats as $cat) { //go thru to find child one - means cat which has specified parent id
    if ($cat->category_parent != 0) {
        $child = $cat->term_taxonomy_id;
echo get_category_parents( $child, TRUE, ' > ' );

First we get categories for particular post, then we find last child and then with get_category_parents we get whole tree of its parents...

  • doesn't work...
    – Towfiq
    Jan 25, 2014 at 19:53
  • this assumes that there is only one subcategory to each parent. in case there are more children and sub-children, this breaks.
    – uncovery
    Jul 20, 2019 at 1:42

Here my working solution for that:

$categories = get_the_category();

$ordering = array();
foreach( $categories as $index => $cat) {
    $ordering[$cat->parent] = $index;

$ordered_string = "";
$i = 0;
while( $ordering[$i] !== null ){
    $ordered_string .= '<li class="item-cat"><a href="'.get_category_link( $categories[$ordering[$i]]->term_id ).'">'.$categories[$ordering[$i]]->name.'</li>';

    $i = $categories[$ordering[$i]]->term_id;

echo $ordered_string;

It echoes the string ready for the breadcrumb, and with a little modification yo can get the categories ordered as array.

  • this works, but only if there just one top-level category. If the post is in several hierarchical categories, one will overwrite the other
    – uncovery
    Jul 20, 2019 at 1:59

Old question but top result on Google for me. I couldn't quickly find a breadcrumb categorization, supporting multiple categories, so I created one and wrapped it in a ul for easy styling:

// breadcrumb categorization
$cats = wp_get_post_categories( get_the_ID() );  //post id
foreach($cats as $c) {
    $ancestors = array_merge( array_reverse( get_ancestors( $c, 'category' ) ), [$c] );
    echo '<ul class="post-categories">';
    foreach($ancestors as $id){
        echo '<li><a href="' . esc_url( get_category_link( $id) ) . '">' . get_cat_name( $id ) . '</a></li>';
    echo '</ul>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.