amazingly, this seems to be hard, and I don't see the answer anywhere online. simply list all the category of the current post (on single.php), but also all the parent categories of it.

<?php the_category( ', ' ); ?>

only lists and links to the subcategory the post is added to.

so, if a post is added to a subcategory called 'apples', and that category has a parent category called 'fruits', show and link to

fruits >> apples

or, if its parent category has a parent category, list and link to that one too

green >> fruits >> apples

simply said, list and link all of them, parent or child.

Update: thanks to @Svartbaard answer for pointing me in the right direction, I passed the category id and it works:

                 $categories = get_the_category();
                 $cat = $categories[0]->cat_ID;
                 echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

I believe you are looking for the following function

<?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

where $cat is the id of the current category. This example if from the codex https://codex.wordpress.org/Function_Reference/get_category_parents

  • thanks for pointing me in the right direction. I passed the category id and it works – whitelord Feb 24 '17 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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