1

I know that we can get the parent category in normal post type by:

get_category_parents($cat, TRUE, ' » ');

But how can we get the parent category in custom post type?

I use this for:

if ( is_category() || is_single() && !is_singular( 'portfolio' ) ) { // Full path links 
    $category = get_the_category();
    $ID = $category[0]->cat_ID;
    echo '<li>'.get_category_parents( $ID, TRUE, '', FALSE ).'</li>';       
} elseif ( is_singular( 'portfolio' ) ) {
    $category = get_the_terms( $post->ID, 'portfolio-category' );
    $ID = $category[0]->cat_ID;
    echo '<li>'.get_category_parents( $ID, TRUE, '', FALSE ).'</li>';
}
  • What exactly is your problem? – fuxia Feb 9 '13 at 12:12
  • I want to display the parent category of the portfolio post – elkebirmed Feb 9 '13 at 12:23
1

Updated answer:

There's now a core function since WordPress 4.8, to list the ancestors of a given term:

get_term_parents_list( 
    int $term_id, 
    string $taxonomy, 
    string|array $args = array() 
)

and get_category_parents() is a wrapper for that function with $taxonomy as 'category'.

Previous answer:

Here is a modified version I made from the function get_category_parents() to support general taxonomies.

/**
 * Retrieve category parents with separator for general taxonomies.
 * Modified version of get_category_parents()
 *
 * @param int $id Category ID.
 * @param string $taxonomy Optional, default is 'category'. 
 * @param bool $link Optional, default is false. Whether to format with link.
 * @param string $separator Optional, default is '/'. How to separate categories.
 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
 * @param array $visited Optional. Already linked to categories to prevent duplicates.
 * @return string
 */
function wpse85202_get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {

            $chain = '';
            $parent = get_term( $id, $taxonomy );

            if ( is_wp_error( $parent ) )
                    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 .= wpse85202_get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
            }

            if ( $link )
                    $chain .= '<a href="' . esc_url( get_term_link( $parent,$taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
            else
                    $chain .= $name.$separator;

            return $chain;
    }

You can use the function like this:

echo wpse85202_get_taxonomy_parents($cat, $tax, TRUE, ' &raquo; ');

or like

echo wpse85202_get_taxonomy_parents(65, 'country', TRUE, ' &raquo; ');
|improve this answer|||||
  • @MattR thanks for your edit, first I accepted it, as I got confused about the word "nicename" for a moment, but it should be nice in the slug-way, so I reverted it again ;-) I also updated the answr with the new core function in 4.8. – birgire Aug 15 '17 at 16:57

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.