Open adrian200065 opened 3 years ago
You need to get the current page url and match it with the current nav item before applying the classes.
Something like,
public function abdpeds_nav_class($classes, $item, $args) {
$current_url = get_permalink();
$active_class = ($item->url === $current_url) ? 'active' : '';
if (in_array('current-menu-item', $classes)) {
$active_class .= ' current-menu-item active-link';
}
$classes[] = $active_class;
return $classes;
}
add_filter('nav_menu_css_class', [ $this, 'abdpeds_nav_class', 10 , 2 ] );
But this is the case if your nav item have current-menu-item present already
I am using this function to highlight active menu but it is not working. Can tell me what I am going wrong? I added the function to the class-menus,php file
public function abdpeds_nav_class( $classes, $item ) {
this is the filter add_filter( 'nav_menu_css_class', [ $this, 'abdpeds_nav_class', 10 , 2 ] );