Last active
February 11, 2024 01:35
-
-
Save davidwebca/a7b278bbb0c0ce1d1ec5620126e863bb to your computer and use it in GitHub Desktop.
Allow adding custom classes to WordPress menu ul, li, a and at different depths. Perfect for TailwindCSS and AlpineJS usage.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Vince! This gist is now a full composer package : https://github.com/davidwebca/wordpress-menu-classes
You can look at the updated file there if you want to use it standalone : https://github.com/davidwebca/wordpress-menu-classes/blob/main/src/WordPressMenuClasses.php
Especially since there were updates to WordPress core that I contributed that allows to add classes and attributes to the links directly (instead of only being able to add them to the LI elements).
What I'm looking at in your linked code seems to be fine unless I don't understand completely what you want to achieve. One thing to note is that if you want to only keep one submenu opened, you need to store an ID of some sort.
Here's a quick demo of how I do it and you can also check out the newest Alpine plugin Anchor which solves a part of the headache for anchoring submenus :
--
Edit: The demo is HTML only but gives you an idea of what would be the attributes to pass to which element in
wp_nav_menu