imranhsayed / aquila

🎨 An Advanced WordPress theme
1.02k stars 218 forks source link

Highlight Active Menu Link #64

Open adrian200065 opened 3 years ago

adrian200065 commented 3 years ago

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 ) {

    if ( in_array( 'current-menu-item', $classes ) ) {
        $classes[] = 'current-menu-item active-link';
    }

    return $classes;
}

this is the filter add_filter( 'nav_menu_css_class', [ $this, 'abdpeds_nav_class', 10 , 2 ] );

joakim-tldr commented 1 year 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