I've used the below code to add the active class to menu items. The site is 99% Ecomm using Woocommerce and I'm using product categories as top level navigation items. The below code works with almost all items except when being active on a product sub-category.
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class ($classes, $item) {
if (in_array('current-post-ancestor', $classes) || in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
Essentially, I'd like the top level nav item to be marked with the active class even when a sub-category has been selected.