-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Description
Generally when you click an item in the mobile version of the nav, the page load causes it to appear the menu has closed.
In the case of someone using a jump link / id # as the target/destination of a custom link to jump down the page, the mobile menu does not close.
A simple/common use case is having the contact link in the nav jump to the footer where all the contact info appears.
I use this on my own site: https://monroeand.co/
Seems like whatever JS controls the mobile nav needs an instruction to close the menu if something is clicked and there's no page reload...?
This code (provided by Claude) worked for me when tweaked with the correct classes and placed in functions.php, but this should be in core with the mobile menu code.
/*
* Close mobile menu when clicking anchors
*/
function add_mobile_menu_close_script() {
?>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('a[href*="#"]').on('click', function(e) {
// Close mobile menu - adjust selectors for your theme
$('body').removeClass('has-modal-open');
$('.wp-block-navigation__responsive-container-close').click();
});
});
})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'add_mobile_menu_close_script');
Step-by-step reproduction instructions
- put a custom link in a nav that jumps down to an ID on the page
- open in narrow browser or mobile
- click the link
- menu will not close since there's no page load
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
- Yes
- Searched for all the keywords I could think of
- Read thru this wonderful NAV tracking issue: Navigation Project Tracking Issue #38275
- Found this mention of it 4 years ago: https://stackoverflow.com/questions/66868710/wordpress-mobile-menu-does-not-close-after-pressing-meniu-item
- This video shows a fix only as it relates to Elementor: https://www.youtube.com/watch?v=HT5x4SvyJG8
Please confirm that you have tested with all plugins deactivated except Gutenberg.
- Yes
Please confirm which theme type you used for testing.
- Block
- Classic
- Hybrid (e.g. classic with theme.json)
- Not sure