How to get parents of custom taxonomy, as what get_category_parents() do?

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?

Related posts

Leave a Reply

5 comments

  1. @Jeff,

    Thanks for this. I fixed your broken $link function. and changed the defaults to include “ยป” as the separator.

    // my own function to wo what get_category_parents does for other taxonomies
    function get_taxonomy_parents($id, $taxonomy, $link = true, $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;
    
    else
    
        $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 ) {
            // $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
            $chain .= '<a href="' . esc_url( get_term_link( (int) $parent->term_id, $taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
        } else {
            $chain .= $name.$separator;
        }
        return $chain;
    
    }
    
  2. 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).

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

  4. 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;
    
    else
    
        $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 ) {
    
        $chain .= '<a href="' . esc_url( get_term_link( (int) $parent->term_id, $taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
    } else {
        $chain .= $name.$separator;
    }
    return $chain;
    
    }