1
votes

I have the following code that generates a list of terms in a taxonomy term, and then POSTS that are under each term.

I want to have a current-page-item class added to the current item, so that when you are on a page under the taxonomy term, its related item in the nav is styled. Here is my code:

<?php $terms = get_terms('benefit-cats');
        echo "<ul>";
        foreach ($terms as $term) {
            $wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug,'order'=>'asc','orderby'=>'title');
            $query = new WP_Query ($wpq);
            echo "<li class=".$term->slug."><span class=\"list-item\"><span class=\"text-arrow\">&#9658;</span> ".$term->name."</span>"; //<a href=\"".get_term_link($term->slug, 'benefit-cats')."\"></a>//
            echo "<ul class=\"children\">";
            ?>

            <?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<li><span class="text-arrow">&#9658;</span> <a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php endwhile; endif; wp_reset_query(); ?>

            <?php   
            echo "</ul></li>";
        }
    echo "</ul>";
?>
1

1 Answers

1
votes

You can try something like this... Specify the current post_id before your loop, then condition to see if post loop contains your post_id.

// before loop
$page_id = $wp_query->get_queried_object_id();

// replace <li><span class="text-arrow">&#9658;</span> 
if($page_id ==$query->post->ID ) $class = " current-page-item";
<li><span class="text-arrow<?php echo $class; ?>">&#9658;</span>