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 found (googled) this function which performs almost exactly as I need. Creating a link text structure (breadcrumb) of terms for a CPT i.e. Parent Category -> Child Category -> Grand Child Category

The only issue is the order is which it is output is slightly off - Child Category -> Parent Category -> Grand Child Category

I cant figure out from the code below how to order it correctly.

Any pointers would be much appreciated.

Many thanks in advance

rob

function get_the_term_list_breadcrumbs( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $breadcrumb_sep = ' → ' ) {
$terms = get_the_terms( $id, $taxonomy );

if ( is_wp_error( $terms ) )
    return $terms;

if ( empty( $terms ) )
    return false;

foreach ( $terms as $term ) {
    $link = get_term_link( $term, $taxonomy );
    if ( is_wp_error( $link ) )
        return $link;

    // Find parents
    $names = array();
    $ancestors = get_ancestors( $term->term_id, $taxonomy );
    if ( count( $ancestors ) > 0 ) {
        foreach ( $ancestors as $anc ) {
            $t = get_term( $anc, $taxonomy );
            $names[] = $t->name;
        }
    }
    $names[] = $term->name;
    $link_text = implode( $breadcrumb_sep, $names );

    $term_links[] = '<a href="' . $link . '" rel="tag">' . $link_text . '</a>';
}

$term_links = apply_filters( "term_links-$taxonomy", $term_links );

return $before . join( $sep, $term_links ) . $after;
}
share|improve this question

1 Answer 1

get_ancestors() returns an array with the parent first, then the grandparent, etc. What you need to do change get_ancestors() to array_reverse( get_ancestors() ), so that you have the level of the parents descends, instead of ascending.

share|improve this answer
    
Thankyou very much, worked perfectly. –  Rob Oliver Mar 12 '12 at 6:47
    
If this was the solution, mark the question as solved. –  m0r7if3r Mar 12 '12 at 21:42

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.