how to add active css class to a link in WordPress

how to add active css class to a link in WordPress such that the active menu li will have different color when that link is viewed.

<ul>
  <li class=""><a href="/news">News</a></li> 
  <li class="devider">&nbsp;</li>
  <li class=""><a href="/about">About Us</a></li> 
  <li class="devider">&nbsp;</li>
  <li><a href="#">Partners</a></li> 
  <li class="devider">&nbsp;</li> 
  <li><a href="/vacancy">Careers</a></li>  <li class="devider">&nbsp;</li>
  <li><a target="_blank" href="/webmail">Email Login</a></li>
</ul>

Related posts

Leave a Reply

2 comments

  1. the code you provided is not WordPress it`s just something you added to your “header.php” but you can try something like this for it:

    $(function() {
      $('ul a[href^="/' + location.pathname.split("/")[1] + '"]').addClass('active');
    });
    

    if you have a default WordPress menu the active class in it is called “.current-menu-item” and if you want to change it’s name to let`s say “.active” find the file called “functions.php” and add this to it:

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    function special_nav_class($classes, $item){
         if( in_array('current-menu-item', $classes) ){
                 $classes[] = 'active ';
         }
         return $classes;
    }