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
By default, `build()` uses `primary_navigation` if no menu location is specified.
40
+
41
+
Retrieving an array of menu items can be done using `all()`:
45
42
46
-
return $navigation->toArray();
43
+
```php
44
+
if ($menu->isNotEmpty()) {
45
+
return $menu->all();
46
+
}
47
47
```
48
48
49
-
When building the navigation menu, Navi retains the menu object and makes it available using the `get()` method.
49
+
> [!NOTE]
50
+
> Check out the [**examples**](examples) folder to see how to use Navi in your project.
50
51
51
-
By default, `get()` returns the raw[`wp_get_nav_menu_object()`](https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object) allowing you to access it directly.
52
+
### Menu Item Classes
52
53
53
-
Optionally, you may pass a `key` and `default` to call a specific object key with a fallback have it be null, empty, or not set.
54
+
By default, Navi removes the default WordPress classes from menu items such as `menu-item` and `current-menu-item` giving you full control over your menu markup while still passing through custom classes.
55
+
56
+
If you would like these classes to be included on your menu items, you may call `withDefaultClasses()` before building your menu:
In some situations, plugins may add their own classes to menu items. If you would like to prevent these classes from being added, you may pass an array of partial strings to `withoutClasses()` match against when building.
61
63
62
-
If you are using Navi alongside [Acorn](https://roots.io/acorn/) (e.g. Sage), you may generate a usable view component using Acorn's CLI:
When building the navigation menu, Navi retains the menu object and makes it available using the `get()` method.
71
+
72
+
By default, `get()` returns the raw [`wp_get_nav_menu_object()`](https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object) allowing you to access it directly.
73
+
74
+
```php
75
+
$menu->get()->name;
66
76
```
67
77
68
-
Once generated, you may use the [view component](https://laravel.com/docs/11.x/blade#components) in an existing view like so:
78
+
Optionally, you may pass a `key` and `default` to call a specific object key with a fallback when the value is blank:
69
79
70
80
```php
71
-
<x-navigation />
81
+
$menu->get('name', 'My menu title');
72
82
```
73
83
74
84
### Accessing Page Objects
75
85
76
86
If your menu item is linked to a page object (e.g. not a custom link) – you can retrieve the ID of the page using the `objectId` attribute.
77
87
78
-
```php
79
-
# Blade
80
-
{{ get_post_type($item->objectId) }}
88
+
Below is an example of getting the post type of the current menu item:
81
89
82
-
# PHP
83
-
<?php echo get_post_type($item->objectId); ?>
90
+
```php
91
+
$type = get_post_type($item->objectId)
84
92
```
85
93
86
94
### Accessing Custom Fields
@@ -90,18 +98,34 @@ In a scenario where you need to access a custom field attached directly to your
90
98
Below we'll get a label override field attached to our menu [using ACF](https://www.advancedcustomfields.com/resources/adding-fields-menus/) – falling back to the default menu label if the field is empty.
Once generated, you may use the [view component](https://laravel.com/docs/11.x/blade#components) in an existing view like so:
113
+
114
+
```php
115
+
<x-menuname="footer_navigation" />
116
+
```
117
+
118
+
To list all registered locations and their assigned menus, you can use the list command:
119
+
120
+
```sh
121
+
$ wp acorn navi:list
98
122
```
99
123
100
124
## Example Output
101
125
102
-
When calling `build()`, Navi will parse the passed navigation menu and return a fluent container containing your menu items. To return an array of objects, simply call `->toArray()`.
126
+
When calling `build()`, Navi will retrieve the WordPress navigation menu assigned to the passed location and build out an array containing the menu items.
103
127
104
-
By default, `build()` calls `primary_navigation` which is the default menu theme location on Sage.
128
+
An example of the menu output can be seen below:
105
129
106
130
```php
107
131
array [
@@ -177,8 +201,6 @@ array [
177
201
]
178
202
```
179
203
180
-
That being said, depending on how deep your menu is– you can ultimately just keep looping over `->children` indefinitely.
181
-
182
204
## Bug Reports
183
205
184
206
If you discover a bug in Navi, please [open an issue](https://github.com/Log1x/navi/issues).
0 commit comments