How to add active class in WordPress bootstrap menu using hook

When we implemented the bootstrap menu in WordPress. you may be frustrated with the built-in menu options. There isn’t an easy way to tell WordPress to swap out the current menu item class with bootstrap’s active class. there is a given below hook. this following script write a script in the fucntion.php file

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;
}

In this example, we are checking if the current menu item has the current-menu-item class, which indicates that it is the current page. If it does, we add the active class to the array of CSS classes for that menu item.

You can add this code to your WordPress theme’s functions.php file to apply the active class to all Bootstrap menus. If you only want to apply the active class to a specific menu, you can modify the code to check for the menu ID or other criteria.

Leave a Reply

Your email address will not be published. Required fields are marked *