aquila icon indicating copy to clipboard operation
aquila copied to clipboard

Highlight Active Menu Link

Open adrian200065 opened this issue 2 years ago • 1 comments

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 ] );

adrian200065 avatar Jul 05 '21 20:07 adrian200065

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

joakim-tldr avatar Aug 19 '23 07:08 joakim-tldr