Retrieving custom taxonomy in order, but excluding specific tax IDs

I have a function set up and working based off the discussion in this thread: Custom taxonomy, get_the_terms, listing in order of parent > child.

My version includes term links and allows me to display the term information in single.php in a fashion that looks like a breadcrumb. However, I have a custom term set up that I never want displayed to users, as it’s a hook used for the content editors so they can display posts at the top of specific loops in other areas of the site.

Previously I’ve had an exclude get_the_term_list function working, but it was not displayed in order.

How can I fix my function so that the terms are displayed in order, but X term is excluded from being showed.

My current function (with exclude currently not working correctly) is as follows:

function terms_by_order( $terms, $taxonomy, $exclude = array() ) {

// check input
if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) )
    return;

// set id variables to 0 for easy check 
$grandparent_id = $parent_id = $term_id = 0;

// get grandparent
 foreach ( $terms as $term ) { 
    if ( $grandparent_id || $term->parent && !($exclude) )
        continue;
    $grandparent_id = $term->term_id;
    $grandparent_slug    = $term->slug;
    $grandparent_url="<a href="".get_term_link($grandparent_slug, $taxonomy).'">'.$term->name.'</a>';
}

 // get parent
foreach ( $terms as $term ) { 
    if ( $parent_id || $grandparent_id != $term->parent && !($exclude) ) 
        continue;
    $parent_id = $term->term_id;
    $parent_slug    = $term->slug;
    $parent_url="<a href="".get_term_link($parent_slug, $taxonomy).'">'.$term->name.'</a>';
}

// get child
foreach ( $terms as $term ) { 
    if ( $parent_id || $parent_id != $term->parent && !($exclude) ) 
        continue;
    $term_id =  $term->term_id;
    $term_slug = $term->slug;
    $term_url="<a href="".get_term_link($term_slug, $taxonomy).'">'.$term->name.'</a>';
}

    echo "$grandparent_url / $parent_url / $term_url";
}

This is the tag I use on my single.php to call this into action

terms_by_order( get_the_terms( $post->ID, 'news_category' ),'news_category',array(3623) );

An example of how this is supposed to look when executing correctly is:

Music / Interview /

1 Answer
1

Alas no answers or comments! 😛

Not to worry, a colleague helped me out here and here is the above code working with an $exclude

function terms_by_order($taxonomy, $exclude) {
global $post;
$terms = get_the_terms($post->ID, $taxonomy);

// check input
if ( empty($terms) || is_wp_error($terms) || !is_array($terms) ) return;

// exclude 
foreach ($terms as $key=>$term) {
    if (in_array($key, $exclude)) {     // key in term_array is also term_id..
        unset($terms[$key]);
        break;
    }
}


foreach ($terms as $key=>$term) {
    $parent_term = $term;           // gets last parent (should we get only the first one?)
    if ($term->parent != 0) {       // if there is a child, find it
        $child_term = $term;        // get the child term...
        $parent_term = get_term_by('id', $term->parent, $taxonomy);     // ... and the parent term
        break;
    }
}

if (isset($parent_term)) echo '<a href="'.get_term_link($parent_term, $taxonomy).'">'.$parent_term->name.'</a>';
if (isset($child_term)) echo ' / <a href="'.get_term_link($child_term, $taxonomy).'">'.$child_term->name.'</a>';

}

Leave a Comment