Add active class in wordpress menu

No comments

I have found tons of queries on discussion forums about 'How to add an active class in the WordPress menu' seems much popular. I also have discovered that there's lots of answers are false or they are not working (as in my case). And of course, there are plenty of answers that are correct. So that, I thought to share my experience and the code that works for me. Here is the complete code, that you have to put in your functions.php file in your active theme to make it work. That's it.

<?php
add_filter('nav_menu_css_class' , 'ninedo_nav_class' , 10 , 2);

function ninedo_nav_class ($classes, $item) {
    if (in_array('current-menu-item', $classes) ){
        $classes[] = 'active ';
    }
    return $classes;
}
?>

In the above example, I have added a hook for modifying WordPress menu behaviour to work as per my requirement. Please do let me know, your views if it works for you.

No comments :

Post a Comment