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 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>';
}
share|improve this question
    
What exactly is your problem? –  toscho Feb 9 '13 at 12:12
    
I want to display the parent category of the portfolio post –  elkebirmed Feb 9 '13 at 12:23
    
any luck so far? –  birgire Feb 13 '13 at 20:26

1 Answer 1

up vote 0 down vote accepted

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; ');
share|improve this answer

Your Answer

 
discard

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.