• Stars
    star
    253
  • Rank 160,776 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A developer-friendly alternative to the WordPress NavWalker.

Navi

Latest Stable Version Total Downloads Build Status

Hate the WordPress NavWalker? Me too.

Navi is a developer-friendly alternative to the NavWalker. Easily build your WordPress menus using an iterable object inside of a template/view.

Requirements

Installation

Bedrock (or Sage)

Install via Composer:

$ composer require log1x/navi

Manual

Download the latest release .zip and install into wp-content/plugins.

Usage

Check out the examples folder to see how to use Navi in your project.

Basic Usage

<?php

use Log1x\Navi\Navi;

$navigation = (new Navi())->build('primary_navigation');

if ($navigation->isEmpty()) {
  return;
}

return $navigation->toArray();

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

By default, get() returns the rawwp_get_nav_menu_object() allowing you to access it directly.

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.

$navigation->get()->name;
$navigation->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.

# Blade
{{ get_post_type($item->objectId) }}

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

Accessing Custom Fields

In a scenario where you need to access a custom field attached directly to your menu item – you can retrieve the ID of the menu item using the id attribute.

Below we'll get a label override field attached to our menu using ACF – falling back to the default menu label if the field is empty.

# Blade
{{ get_field('custom_nav_label', $item->id) ?: $item->label }}

# PHP
<?php echo get_field('custom_nav_label', $item->id) ?: $item->label; ?>

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

By default, build() calls primary_navigation which is the default menu theme location on Sage.

array [
  5 => {
    +"active": true
    +"activeAncestor": false
    +"activeParent": false
    +"classes": "example"
    +"dbId": 5
    +"description": false
    +"id": 5
    +"label": "Home"
    +"objectId": "99"
    +"parent": false
    +"slug": "home"
    +"target": "_blank"
    +"title": false
    +"url": "https://sage.test/"
    +"xfn": false
    +"order": 1
    +"parentObjectId": false
    +"children": false
  }
  6 => {
    +"active": false
    +"activeAncestor": false
    +"activeParent": false
    +"classes": false
    +"dbId": 6
    +"description": false
    +"id": 6
    +"label": "Sample Page"
    +"objectId": "100"
    +"parent": false
    +"slug": "sample-page"
    +"target": false
    +"title": false
    +"url": "https://sage.test/sample-page/"
    +"xfn": false
    +"order": 2
    +"parentObjectId": false
    +"children": array [
      7 => {
        +"active": false
        +"activeAncestor": false
        +"activeParent": false
        +"classes": false
        +"dbId": 7
        +"description": false
        +"id": 7
        +"label": "Example"
        +"objectId": "101"
        +"parent": 6
        +"slug": "example"
        +"target": false
        +"title": false
        +"url": "#"
        +"xfn": false
        +"order": 3
        +"parentObjectId": 100
        +"children": 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.

Contributing

Contributing whether it be through PRs, reporting an issue, or suggesting an idea is encouraged and appreciated.

License

Navi is provided under the MIT License.

More Repositories

1

acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
PHP
310
star
2

acf-builder-cheatsheet

A cheatsheet for use with ACF Builder.
262
star
3

sage-directives

A set of Blade directives for use with Roots Sage.
PHP
235
star
4

poet

Configuration-based post type, taxonomy, block category, and block registration for Sage 10.
PHP
148
star
5

sage-svg

A simple package for using inline SVGs with Sage 10.
PHP
91
star
6

acf-phone-number

A real ACF phone number field powered by libphonenumber and intl-tel-input
PHP
91
star
7

acf-editor-palette

A Gutenberg-like editor palette color picker field for Advanced Custom Fields.
PHP
80
star
8

blade-svg-sage

A simple package to add support for Blade SVG by Adam Wathan to Roots Sage.
PHP
71
star
9

modern-login

A whitelabeled and modernized wp-login.php
CSS
69
star
10

acf-field-boilerplate

Modernized PSR-2 boilerplate for creating custom fields for ACF5.
PHP
58
star
11

socialproof

A fluent interface for retrieving follower counts from social API's.
PHP
39
star
12

tailwindcss-pseudo

Tailwind CSS plugin to generate pseudo selector variants.
JavaScript
38
star
13

sage-html-forms

Create forms using HTMLForms.io and Sage 10 Blade components
PHP
37
star
14

pagi

A better WordPress pagination.
PHP
36
star
15

wp-smtp

Simple package for handling WordPress SMTP with .env when using the Roots stack.
PHP
33
star
16

acf-move-wp-editor

This is a simple ACF Field that moves the WordPress content editor of a post or page to the location of this field.
PHP
32
star
17

modern-acf-options

A modern approach to ACF Theme Options
CSS
30
star
18

crumb

A simple breadcrumb package for Sage 10.
PHP
29
star
19

bulma.styl

A Stylus translation of a modern CSS framework based on Flexbox
CSS
26
star
20

sage-password-protected

A simple password protection package for use with Roots Sage.
PHP
18
star
21

tailwindcss-container-sizes

Simple Tailwind plugin to generate container sizes
JavaScript
18
star
22

acf-fluent-helpers

Simple set of helper functions for ACF Fluent auto-loaded with Composer using generic function names.
PHP
14
star
23

wordpress-ui

Here lives a modernized approach to the WordPress Admin UI
CSS
14
star
24

sage-eject-blocks

Eject Sage 10 editor scripts to a optional lazy-loaded plugin.
PHP
13
star
25

plugin-meta

A simple meta package to install a few plugins I use regularly along with a couple related filters.
PHP
13
star
26

acf-blocks

PHP
12
star
27

docker-rtorrent-flood

Dockerfile
11
star
28

tailwindcss-border-styles

Tailwind CSS plugin to generate individual border side style classes.
JavaScript
8
star
29

acf-sidebar-selector

PHP
7
star
30

acf-encrypted-password

A better ACF Password Field that is encrypted using bcrypt.
PHP
7
star
31

gutentweaks

JavaScript
6
star
32

pomf

A simple implementation of the Pomf API.
PHP
4
star
33

acf-move-yoast

This is a simple ACF Field that moves the Yoast SEO metabox to the location of this field.
PHP
4
star
34

envoyer-deploy-commands

Simple deployment with Envoyer using Artisan
PHP
3
star
35

dotfiles

My personally used dotfiles on macOS
PHP
3
star
36

docker-mopidy-iris

Dockerfile
2
star
37

laravel-mix-jigsaw

Jigsaw's build tasks written as a Laravel Mix plugin.
JavaScript
2
star
38

nofollow-pretty-links

PHP
2
star
39

redirection-variables

PHP
2
star
40

wp-offload-media-avatars

A simple WP Media Offload integration for local avatar plugins.
PHP
2
star
41

rom-cli

A simple CLI tool to fetch prices from ROM Exchange
PHP
1
star
42

android

My personally used Android setup
1
star
43

allow-unsafe-links

Prevents WordPress from automatically adding noopener and noreferrer to outbound links.
PHP
1
star
44

acf-move-related-posts

This is a simple ACF Field that moves the Related Posts for WordPress metabox to the location of this field.
PHP
1
star
45

log1x.com

The current iteration of my personal website.
Vue
1
star
46

blade-filetype-icons

A package to easily make use of dmhendricks/file-icon-vectors in your Laravel Blade views.
PHP
1
star