WordPress display taxonomy terms in loop

My aim is pretty simple but couldn’t find a solution anywhere : when someone clicks on one of the taxonomy terms I have created for a custom post it brings them as expected to taxonomy.php. On this taxonomy.php page I want to display all the taxonomy terms in a list and assign dynamically a class called ACTIVE to the chosen term by the user.
I know I could create pages like taxonomy-slug.php for each term but I prefer something more dynamic.

So when someone clicks on the THRILLER term from the MOVIES taxonomy I would like to have THRILLER listed with the CSS class = “active”

Read More

At the moment terms are displayed on the page but the current one never has the class assigned (first case in my code). Can someone tell me what’s wrong with the code ?

Thx in advance

<?php
    $termqueried =  $wp_query->queried_object;
    $taxo = $termqueried->name;
    $terms = get_terms('MOVIES'); 
    foreach ($terms as $term) {
    if ($taxo=$terms->name) {
    echo '<li class="active">'.$term->name.'</li>';
    }
    else { echo '<li>'.$term->name.'</li>'; }
    }
    ?>

Related posts

Leave a Reply

1 comment

  1. Are you simply not getting confused by your use of the similar words of ‘terms’ and ‘term’. Like the following:

    if ($taxo=$terms->name) {
    

    has to be:

    if ($taxo=$term->name) {
    

    Hope this was it.