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 /
Alas no answers or comments! 😛
Not to worry, a colleague helped me out here and here is the above code working with an $exclude