I’m currently using get_terms to list terms of a certain taxonomy. I would like to add a class to the term currently being queried via the archive template.
I tried putting together the below code but this bizarrely results in the class being applied to not just the current term being queried but every term AFTER it as well. I’ve tried comparing the slugs, ids and names of $term and $currentterm – all of which output correctly.
$terms = get_terms('MYTAX');
$currentterm = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
foreach ($terms as $term) {
if($currentterm->slug == $term->slug) $class = "live";
echo '<li class="'. $class .'"><a href="http://website.com/?MYTAX='. $term->slug .'">' . $term->name . '</a></li>';
}
Try this instead:
Basically you just need to reset your
$class
variable back to an empty string after your conditional has returned false, otherwise it will continue to remain true. Its seems odd, but once you know how to handle it, its all good.