To add a class to submenu items when they are clicked using jQuery in WordPress, you can use the following code. This assumes that you are using the default WordPress navigation menu structure:
This jQuery code targets all
<li> elements within a submenu (
'.sub-menu li'). When a submenu item is clicked, it removes the ‘active’ class from all submenu items and adds the ‘active’ class to the clicked item.
wp_enqueue_script function in your theme’s
Make sure to replace
After enqueuing the script, don’t forget to check your browser’s console for any errors and ensure that jQuery is properly loaded on your site.