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>';
}
Updated answer:
There’s now a core function since WordPress 4.8, to list the ancestors of a given term:
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.You can use the function like this:
or like