Show nav link highlighted

OK so i have a main menu and a sub-menu

How can i make a menu item active when viewing a single post.

Read More

The sub-menu uses taxonomies so i know i need to make the taxonomy active when one of the posts has the taxonomy in use

Related posts

Leave a Reply

4 comments

  1. I did something similar with highlighting current category in navbar, you can see my answer here. The hook of course won’t work for you, I think a better hook would be ‘nav_menu_css_class’

  2. So i ended up doing some jquery and lots of it. The downside is that i have to add the code each time i create a new menu. I am looking into re-creating this but do not want to bother with it right now. This is the link to the solution. It is very very temporary so if you wish to use it go ahead but there are better ways. You may be able to hook into the wp_nav_menu classes and add a active class to the current active item and then use some jquery to finish it off.

  3. You can check the slug and and the current taxonomy against one another.

    For example if you were outputting a menu using get_terms($taxonomy) you could use an if statement to check whether the current page’s taxonomy is the same as the slug menu item and give it an active class.

                <?
                    $taxonomy = 'disciplines';
                    $tax_terms = get_terms($taxonomy);
                    $current_taxonomy = get_query_var($wp_query->query_vars['taxonomy']);
                    foreach ($tax_terms as $tax_term) {
    
                        if ($current_taxonomy == $tax_term->slug) {
                            $active = 'active';
                        }
    
                        echo '<li class="filter ' . $active . ' clearfix"><a href="/disciplines/' . $tax_term->slug . '"><i class="fa fa-circle-o"></i><i class="fa fa-circle"></i> ' . $tax_term->name . '</a></li>';
    
                        $active = "";
                    }
                ?>
    

    Where discipline is your custom taxonomy.