Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 1 down vote accepted
<?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.

share|improve this answer

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...

share|improve this answer
    
doesn't work... –  Towfiq Jan 25 at 19:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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