-
-
Save davidwebca/a7b278bbb0c0ce1d1ec5620126e863bb to your computer and use it in GitHub Desktop.
@hofmannsven No worries! Don't forget to check out the package this gist has evolved into. I added a few more functionalities recently. https://github.com/davidwebca/wordpress-menu-classes
@davidwebca Hi there! The script is exactly what I was looking for. Unfortunately I am quite new to wordpress theme dev. How can I activate this functionality if I dont use composer? I added the file to my theme, but that doesn't seem to change anything.
@nikolailehbrink Hi! You can basically copy the whole content of the file here: https://github.com/davidwebca/wordpress-menu-classes/blob/main/src/WordPressMenuClasses.php
and add it to your functions.php (without the first line with the namespace)
Thanks for the response! @davidwebca Okay, that's what I thought, but I find that a little messy. Is there a better way with separation of files?
Using composer would enable the best separation of files, but otherwise you can simply put WordPressMenuClasses.php in your theme wherever and include it from your functions.php 🤔
<?php
/**
* At the top of your functions.php file
*/
require_once('WordPressMenuClasses.php');
@davidwebca Worked fine! Thank you! :)
Great glad to hear!
HI David,
It's really a great job, and it' really usefull. I hop in the future put walker's in the trash...
I'm near to have the best menu of the worrld, but I am encountering a problem : https://github.com/Vince-ALIEN/dev/blob/main/the-header-content.php.
I use Tailwind css and Alpine js.
It's a offcanvas menu on desktop and mobile, the burger open menu w-1/2 and li_atts_0 push it to w-full.
First, i would liketo close the submenu when i click on other li_atts_0 to open them, and then i want to keep the menu full width.
Does your response to @jansolo76 it's the way to realise this ?
Do you have just an idea ? It'would be great...
Encore merci.
Vincent.
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 :
- https://jsfiddle.net/davidwebca/os4guzvy/17/
- https://twitter.com/calebporzio/status/1720147531546447897
--
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
@davidwebca Thanks a lot! 👍