Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">    
        <?php       
        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>";
                }
            }   
        ?>
    </ul>
share|improve this question

1 Answer 1

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

 
discard

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.