I have a custom taxonomy ‘portfolio category’, while i need to show all hierarchy categories of a portfolio post, for example
the post is under topcat -> childcat -> yetchildcat
, i want to show ‘topcat, childcat, yetchildcat’ on the post page, but get_the_terms only
return ‘yetchildcat
‘, how do i get all?
There is a more generic function: it’s called
get_ancestors()
and it’s found inwp-includes/taxonomy.php
@Jeff,
Thanks for this. I fixed your broken $link function. and changed the defaults to include “ยป” as the separator.
There is no deeper level function in
get_category_parents()
(see source). It just checksparent
field in category object and then recursively checks for its parent until it runs out.So you will need either to write simple analogue or rewrite it (depending on how much functionality you need).
get_the_terms
andget_term
retrieve the parent ID amongst the term’s data. So you could use that info to build a custom functions that will display the taxonomy tree.I’m actually working on something similar – will post the code as soon as I have it working and clean ;-D
EDIT : I finally did it… Here is the code to get “topcat -> childcat -> yetchildcat” with link to the taxonomy term page:
I’m sure it is possible to make better/cleaner code, but it seems to work ;-D
I wrote a recursive function based almost exactly off of get_category_parents(), just replacing the category-specific parts with term-based equivalents. Hope it helps someone.