• Resolved tsbain

    (@tsbain)


    I’ve successfully added a class of ‘is-active’ to an active sidebar menu item. My issue is, this adds the class to the ‘li’ element, whereas I need to add it to the ‘a’ element. Any thoughts on how I can accomplish this?

    Here is my code:

    add_filter('nav_menu_css_class' , 'current_item_nav_class' , 10 , 2);
    function current_item_nav_class($classes, $item){
         if( in_array('current-menu-item', $classes) ){
                 $classes[] = 'is-active ';
         }
         return $classes;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,
    I’m not sure what would be the proper way to do it but adding the class with jQuery on client side is easy:

    jQuery(document).ready(function($) {
      $('li.is-active').children('a').addClass('is-active');
    });

    You can also remove the class from the parent li:

    jQuery(document).ready(function($) {
      $('li.is-active').removeClass('is-active').children('a').addClass('is-active');
    });

    Hope that helps.

    Thread Starter tsbain

    (@tsbain)

    Thanks, MyThemeShop! I’ll see if I can make that go.

    Thread Starter tsbain

    (@tsbain)

    MyThemeShop –

    This worked beautifully — thanks again!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Adding new class to active menu item’ is closed to new replies.