I am adding a specific class on my wp_nav_menu via function.php but I am not able to target a specific menu: This is what I got
function add_menuclass_active($ulclass) {
return preg_replace('/<a /', '<a class="list-group"', $ulclass, 1);
}
add_filter('wp_nav_menu', 'add_menuclass_active');
I tried also using with no luck
function add_menuclass_active($ulclass) {
if( $ulclass['theme_location'] == 'CUSTOM MENU' )
return preg_replace('/<a /', '<a class="list-group"', $ulclass, 1);
}
add_filter('wp_nav_menu', 'add_menuclass_active');
Thanks to an answer from wordpress stack I got this solution:
add this to your functions.php
at last you must select the option "Menu1" for the specific menu on which you have to add the anchor custom classes from dashboard Apperance->menus. [select menu2 or menu3 for other menus whose anchor links does not need the custom-class]
To add "active class" to the first menu item of the particular menu then try this one:
if the active class must be added to the first menu item of all menus then use this:
Theme location is stored in the nav menu arguments, you need to add it in your function parameters: