WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code to display a list of taxonomies in Wordpress. It works and my only concern is the active class (li class="active"). Err, how do I go about making it dynamic?

        <ul id="filter">    
        echo '<li class="active"><a href="#" class="all">All</a></li>'; # this remains static and unchanged             
            if ( $count > 0 ) {             
                foreach ( $mylinks as $mylink ) {
                    echo '<li>';
                    # get current webdirectory and link to taxonomy menus
                    echo'<a href=' . network_site_url() . '/services/' .$mylink->slug . ' class='. $mylink->slug .'" >';
                    echo $mylink->name;
                    echo "</a>";
                    echo "</li>";
share|improve this question

Have no idea what is $link->slug is it a post type property? or taxonomy? Something else? in case of something else you can use a strpos in comparing url?

echo'<a href=' . network_site_url() . '/services/' .$mylink->slug . ' class='. ($mylink->slug) 
    . (stripos($_SERVER['REQUEST_URI'], $mylink->slug) !== false ? ' active ':'').'" >';

in case of post use get_post_field

global $wp_query; // little trick...
echo'<a href=' . network_site_url() . '/services/' .$mylink->slug . ' class='. ($mylink->slug) 
    .($mylink->slug === get_post_field('slug', get_the_ID(), false) ? ' active ':'').'" >';

in case of this is a taxonomy archive page using a query object (actualy you can use it in axample above too)...

global $wp_query; // little trick...
echo'<a href=' . network_site_url() . '/services/' .$mylink->slug 
    . ' class='. ($mylink->slug) .($mylink->slug === $wp_query->queried_object->slug ? ' active ':'').'" >';
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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