Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight Active Menu Link #64

Open
adrian200065 opened this issue Jul 5, 2021 · 1 comment
Open

Highlight Active Menu Link #64

adrian200065 opened this issue Jul 5, 2021 · 1 comment

Comments

@adrian200065
Copy link

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
Copy link

joakim-tldr commented Aug 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants