Reload page from main menu
In this version, the jQuery selector targets all a
elements within li
elements that are direct children of any nav
element. This should work with most WordPress themes, as it’s common to use nav
for navigation menus and li
for individual menu items.
Please note that this will cause a page reload for every link in your navigation menu. If you want to exclude certain links, you’ll need to add a specific class to those menu items and adjust the jQuery selector accordingly.
Remember to replace this code in your theme’s functions.php file or in a custom plugin. Always backup your site before making changes to the code. If you’re not comfortable with coding, consider hiring a professional developer.
// Reload active main menu item
function custom_js_footer_code(){
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("nav li a").click(function(){
location.reload(true);
});
});
</script>
<?php
}
add_action('wp_footer', 'custom_js_footer_code');