Wrong active menu items on a one-page WordPress site
If a menu has links with anchors to sections of the current page (/#about, /#contact), WordPress marks all of them as active on that page. It does not have to be a one-page site: any page whose menu links point to anchors on that same page has the problem.
Problem: On the page that contains the anchors, every anchor item in the menu gets current-menu-item and is highlighted as active.
Solution: Remove the active classes from anchor items on the server, with the nav_menu_css_class filter.
Why it happens
When WordPress decides which menu items are current, it cuts off the part after # in custom links. https://example.com/#about and https://example.com/#contact both become https://example.com/, match the current URL and get current-menu-item.
The jQuery way and its drawbacks
The first solution I found was jQuery: remove the classes from anchor items and set them on click.
$( document ).ready( function () {
$( '.nav li a[href*="#"]' ).each( function () {
$( this ).parent().removeClass( 'current-menu-item current-menu-ancestor' );
$( this ).on( 'click', function () {
$( this ).closest( 'ul' ).children( 'li' ).removeClass( 'current-menu-item current-menu-ancestor' );
$( this ).parent().addClass( 'current-menu-item current-menu-ancestor' );
} );
} );
} );
It has several drawbacks:
- To avoid flicker, the script and jQuery itself have to be loaded in the
<head>, which slows down page rendering. - If the scripts are in the footer, the page first appears with all anchor items highlighted and a moment later the script removes the classes. It does not look good.
It is cleaner to fix it on the backend, so the wrong classes never reach the browser.
Way 1. The nav_menu_css_class filter
In functions.php:
<?php
add_filter( 'nav_menu_css_class', 'remove_active_class_from_anchor_items', 10, 2 );
function remove_active_class_from_anchor_items( $classes, $item ) {
if ( false !== strpos( $item->url, '#' ) ) {
$classes = array_diff( $classes, array( 'current-menu-item', 'current_page_item', 'current-menu-ancestor' ) );
}
return $classes;
}
This also works with most custom Walkers, because a Walker built on Walker_Nav_Menu calls the same filter in start_el().
Way 2. Inside a custom Walker
If your Walker builds the classes without the filter, remove them where the class list is built. Your Walker may look different, but the idea is the same:
<?php
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
// Anchor links are never "current".
if ( false !== strpos( $item->url, '#' ) ) {
$classes = array_diff( $classes, array( 'current-menu-item', 'current_page_item', 'current-menu-ancestor' ) );
}
$class_names = ' class="' . esc_attr( implode( ' ', array_filter( $classes ) ) ) . '"';
// ... the rest of the Walker code
}
}
NOTE: If you want to highlight the section that is visible on screen, do it with a small scroll script on top of this fix. Then nothing is marked active by WordPress, and the script only adds a class.
Sources: