Skip to content

Commit

Permalink
📝 Improve the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Oct 1, 2024
1 parent a58d90f commit 8270814
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,67 @@ Download the [latest release](https://github.com/Log1x/navi/releases/latest) `.z

## Usage

Check out the [**examples**](examples) folder to see how to use Navi in your project.

### Basic Usage
Building your menu can be done by passing your menu location to `Navi::make()->build()`:

```php
<?php

use Log1x\Navi\Navi;

$navigation = Navi::make()->build('primary_navigation');
$menu = Navi::make()->build('primary_navigation');
```

if ($navigation->isEmpty()) {
return;
}
By default, `build()` uses `primary_navigation` if no menu location is specified.

Retrieving an array of menu items can be done using `all()`:

return $navigation->toArray();
```php
if ($menu->isNotEmpty()) {
return $menu->all();
}
```

When building the navigation menu, Navi retains the menu object and makes it available using the `get()` method.
> [!NOTE]
> Check out the [**examples**](examples) folder to see how to use Navi in your project.
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.
### Menu Item Classes

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.
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.

If you would like these classes to be included on your menu items, you may call `withDefaultClasses()` before building your menu:

```php
$navigation->get()->name;
$navigation->get('name', 'My menu title');
$menu = Navi::make()->withDefaultClasses()->build();
```

### Acorn Usage
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.

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:
```php
$menu = Navi::make()->withoutClasses(['shop-'])->build();
```

```sh
$ acorn make:navi
### Accessing Menu Object

When building the navigation menu, Navi retains the menu object and makes it available using the `get()` method.

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.

```php
$menu->get()->name;
```

Once generated, you may use the [view component](https://laravel.com/docs/11.x/blade#components) in an existing view like so:
Optionally, you may pass a `key` and `default` to call a specific object key with a fallback when the value is blank:

```php
<x-navigation />
$menu->get('name', 'My menu title');
```

### Accessing Page Objects

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.

```php
# Blade
{{ get_post_type($item->objectId) }}
Below is an example of getting the post type of the current menu item:

# PHP
<?php echo get_post_type($item->objectId); ?>
```php
$type = get_post_type($item->objectId)
```

### Accessing Custom Fields
Expand All @@ -90,18 +98,34 @@ In a scenario where you need to access a custom field attached directly to your
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.

```php
# Blade
{{ get_field('custom_nav_label', $item->id) ?: $item->label }}
$label = get_field('custom_menu_label', $item->id) ?: $item->label;
```

### Acorn Usage

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:

# PHP
<?php echo get_field('custom_nav_label', $item->id) ?: $item->label; ?>
```sh
$ wp acorn navi:make Menu
```

Once generated, you may use the [view component](https://laravel.com/docs/11.x/blade#components) in an existing view like so:

```php
<x-menu name="footer_navigation" />
```

To list all registered locations and their assigned menus, you can use the list command:

```sh
$ wp acorn navi:list
```

## Example Output

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()`.
When calling `build()`, Navi will retrieve the WordPress navigation menu assigned to the passed location and build out an array containing the menu items.

By default, `build()` calls `primary_navigation` which is the default menu theme location on Sage.
An example of the menu output can be seen below:

```php
array [
Expand Down Expand Up @@ -177,8 +201,6 @@ array [
]
```

That being said, depending on how deep your menu is– you can ultimately just keep looping over `->children` indefinitely.

## Bug Reports

If you discover a bug in Navi, please [open an issue](https://github.com/Log1x/navi/issues).
Expand Down

0 comments on commit 8270814

Please sign in to comment.