I'm using this code but it's not working

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

Inspect Element:

<li id="menu-item-106" class="d-inline-block menu-item menu-item-type- 
post_type menu-item-object-page current-menu-item page_item page-item-32 
 current_page_item menu-item-106 active"><a href="#">Something</a></li>

Add active class but still it doesn't change the design still the same.

  • The code works fine and you can see it in the Inspector. First, clean up all caches and refresh the page. Second, check your CSS. – Max Yudin Jul 25 '18 at 14:44

You wont need that PHP code to style links to the current page in your nav. WordPress adds a class (current-menu-item) by default on links to the current page. But WordPress does not style the link for you. That would be part of the theme. You can add a rule to the theme's CSS ( typically /wp-content/themes/your_theme/style.css ) to style the link differently. For example :

li.current-menu-item a {
    color: pink;
    // or whatever
  • Thnx for the answer.Just checked the style.css and saw that current-menu-item is nowhere. – TestimiUEB Jul 26 '18 at 7:19
  • @TestimiUEB You may want to reread my answer : "WordPress does not style the link for you" ... "You can add a rule" – AyexeM Feb 26 '19 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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