3

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

1

Alas no answers or comments! :P

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>';

}
2
  • Could you please mark this as the solution?
    – kaiser
    Commented Jun 10, 2013 at 1:23
  • Sorry about that, marked now.
    – Ashkas
    Commented Jun 10, 2013 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.