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.

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?

share|improve this question

4 Answers 4

There is a more generic function: it's called get_ancestors() and it's found in wp-includes/taxonomy.php

share|improve this answer
Shiny... I keep coming upon nice new stuff in couple latest major releases. –  Rarst Jul 3 '11 at 10:45

There is no deeper level function in get_category_parents() (see source). It just checks parent 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).

share|improve this answer

get_the_terms and get_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:

$terms = get_the_terms( $post->id, 'portfolio category' );
if ( $terms && ! is_wp_error( $terms ) ) { 
    foreach ( $terms as $term ) {
        $tree = '<a href="'.get_term_link($term->slug, 'portfolio category').'">'.$term->name.'</a>';
        $parents = get_ancestors( $term->term_id, 'portfolio category' );
        foreach ($parents as $parent) {
            $term = get_term($parent, 'portfolio category');
            $tree = '<a href="'.get_term_link($term->slug, 'geo').'">'.$term->name.'</a> -> '.$tree;
    echo $tree;

I'm sure it is possible to make better/cleaner code, but it seems to work ;-D

share|improve this answer

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.

// my own function to wo what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {

$chain = '';

$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) { echo "fail";
    return $parent;

if ($nicename)

    $name = $parent -> slug;


    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {

    $visited[] = $parent -> parent;

    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);


if ($link) {
    // nothing, can't get this working :(
} else

    $chain .= $name . $separator;

return $chain;

share|improve this answer

Your Answer


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.