WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] get_category_parents for custom post type taxonomy

[Resolved] get_category_parents for custom post type taxonomy

  • 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' ) ) {
        $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>';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    @keesiemeijer

    I would just copy the function from core and put in your theme’s functions.php. Change the function name and make changes so it gets the terms from the custom taxonomy. Something like this [untested]:

    function get_custom_category_parents( $id, $taxonomy = false, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
    
    	if(!($taxonomy && is_taxonomy_hierarchical( $taxonomy )))
    		return '';
    
    	$chain = '';
    	// $parent = get_category( $id );
    	$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 .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    		$chain .= get_custom_category_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;
    }

    Now you can call this function from your template files like this:

    <?php
    	$category = wp_get_object_terms( $post->ID, 'portfolio-category', array('fields' => 'ids') );
    	if($category)
    		echo '<li>'.get_custom_category_parents( $category[0], 'portfolio-category', TRUE, '', FALSE ).'</li>';
    ?>

    Thank you keesiemeijer. i already do it the easy way:

    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_term_list( $post->ID, 'portfolio-category', '', ', ', '' );
    		echo '<li>Portfolio: ' . $category . '</li>';
    }

    But i will try yours, i’m sure it’s awesome

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] get_category_parents for custom post type taxonomy’ is closed to new replies.
Skip to toolbar