You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-20Lines changed: 38 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# WordPress Menu Classes
2
2
3
-
Allow adding custom classes to WordPress menu ul, li, a and at different depths. Perfect for TailwindCSS and AlpineJS usage.
3
+
Allow adding custom classes to WordPress li, a and submenus at different depths. Perfect for TailwindCSS and AlpineJS usage.
4
4
5
5
This package adds WordPress filters to allow custom arguments to wp_nav_menu to, in turn, allow custom classes to every element of a menu. You can apply a class only to certain depth of your menu as well.
If your theme already uses composer, the filters will be automatically added thanks to the auto-loading and auto-instantiating class. Otherwise, if you're looking for a standalone file you want to add to your theme, either look for src/WordPressMenuClasses.php in this repository or add this [gist](https://gist.github.com/davidwebca/a7b278bbb0c0ce1d1ec5620126e863bb) in your theme's functions.php.
21
+
If your theme already uses composer, the filters will be automatically added thanks to the auto-loading and auto-instantiating class. Otherwise, if you're looking for a standalone file you want to add to your theme, either look for src/WordPressMenuClasses.php in this repository.
22
22
23
23
## Instructions
24
24
25
25
The filters use the depth argument given by WordPress which is an index, thus starts with level 0 (zero).
26
26
27
27
Here's a list of the custom arguments you can pass to wp_nav_menu that are supported by this package :
28
28
29
-
-```link_atts``` or ```link_atts_$depth``` or ```link_atts_order_$order```
30
-
- Add any attribute to ```<a>``` elements
29
+
-```a_atts``` or ```a_atts_$depth``` or ```a_atts_order_$order```
31
30
-```a_class``` or ```a_class_$depth``` or ```a_class_order_$order```
32
-
- Add classes to ```<a>``` elements
31
+
- Add any attribute or class to ```<a>``` elements
32
+
33
+
-```li_atts``` or ```li_atts_$depth``` or ```li_atts_order_$order```
33
34
-```li_class``` or ```li_class_$depth``` or ```li_class_order_$order```
34
-
- Add classes to ```<li>``` elements
35
+
- Add any attribute or class to ```<li>``` elements
36
+
37
+
-```submenu_atts``` or ```submenu_atts_$depth```
35
38
-```submenu_class``` or ```submenu_class_$depth```
36
-
- Add classes to submenu ```<ul>``` elements
39
+
- Add any attribute or class to submenu ```<ul>``` elements. Note that submenus do not support order.
37
40
38
41
Ex.: add a "text-black" class to all links and "text-blue" class only to 3rd level links
39
42
@@ -46,24 +49,39 @@ wp_nav_menu([
46
49
]);
47
50
```
48
51
49
-
Ex.: More complete example with some TailwindCSS classes and AlpineJS sugar
52
+
Ex.: Supports classes as array (this is non-native to WordPress, but provided by this package as a convenience)
53
+
54
+
```php
55
+
wp_nav_menu([
56
+
'theme_location' => 'primary_navigation',
57
+
'a_class' => ['text-white', 'bg-blue-500'],
58
+
'li_atts' => [
59
+
'class' => ['focus:ring-2', 'ring-orange-500']
60
+
]
61
+
// ...
62
+
]);
63
+
```
64
+
65
+
Ex.: More complete example with some TailwindCSS classes and AlpineJS sugar. This is a fully functional accordion navigation without additional JavaScript (requires Alpine's x-collapse plugin).
0 commit comments