navi

repository·master·Indexed 18 days ago

https://github.com/log1x/navi

A developer-friendly alternative to WordPress NavWalker that allows building menus using an iterable object. It provides tools for cleaner markup, custom class configuration, and access to page and custom field data. Includes support for Acorn (Sage) with CLI commands like navi:list and navi:make for generating Blade view components, as well as a Laravel facade for easy API access.

Tokens
3.3K
Snippets
18
Records
19
Agent score
63%

What's inside navi

  1. Access Page and Custom Field data from Menu Items

    master

    When iterating through the menu items returned by all(), you can access specific attributes on each $item:

    • Page/Post ID: Use $item->objectId to get the ID of the linked page or post. This is useful for functions like get_post_type().
    • Custom Fields: Use $item->id (the menu item's unique ID) to retrieve custom fields attached to that specific menu item via plugins like ACF.
    // Get the post type of the linked page
    $type = get_post_type($item->objectId);
    
    // Get a custom field (e.g., via ACF) attached to the menu item
    $label = get_field('custom_menu_label', $item->id) ?: $item->label;
  2. Build a WordPress menu with Navi::build()

    master

    To build a menu, use Navi::make()->build() and pass the WordPress menu location (e.g., 'primary_navigation'). If no location is specified, it defaults to primary_navigation.

    To retrieve the resulting array of menu items, use the all() method. It is recommended to check if the menu is not empty using isNotEmpty() first.

    use Log1x
    avi\Navi;
    
    // Build the menu for a specific location
    $menu = Navi::make()->build('primary_navigation');
    
    // Retrieve the array of items
    if ($menu->isNotEmpty()) {
        $items = $menu->all();
    }
  3. Use Navi with Acorn (Sage)

    master

    If using Acorn, you can use the CLI to generate Blade view components for your menus.

    1. Generate a component: wp acorn navi:make Menu
    2. Use in Blade: <x-menu name="footer_navigation" />
    3. List locations: Use wp acorn navi:list to see all registered locations and their assigned menus.
    # Generate a component named 'Menu'
    $ wp acorn navi:make Menu
    
    # List all registered menu locations
    $ wp acorn navi:list
  4. Configure menu item classes

    master

    By default, Navi strips standard WordPress classes (like menu-item or current-menu-item) to give you full control over markup.

    • To include default WordPress classes, call withDefaultClasses() before build().
    • To prevent specific plugin-added classes from appearing, use withoutClasses() and pass an array of partial strings to match against.
    // Include default WordPress classes
    $menu = Navi::make()->withDefaultClasses()->build();
    
    // Exclude specific classes (e.g., those starting with 'shop-')
    $menu = Navi::make()->withoutClasses(['shop-'])->build();
  5. Access the underlying Menu Object

    master

    Navi retains the original WordPress menu object. You can access it using the get() method.

    • Calling $menu->get() returns the raw object from wp_get_nav_menu_object().
    • You can pass a key and a default value to get() to safely retrieve specific object properties.
    // Get the raw menu object
    $menuObject = $menu->get();
    $name = $menuObject->name;
    
    // Get a specific key with a fallback
    $name = $menu->get('name', 'My menu title');
  6. Configure menu item classes via the Navi Facade

    master

    You can control which CSS classes are applied to menu items using the following methods on the Navi facade:

    • withClasses(string|array $classes): Adds specific classes to the menu items.
    • withoutClasses(string|array $classes): Removes specific classes from the menu items.
    • withDefaultClasses(): Applies the default set of classes defined by the package.
    use Log1x\Navi\Facades\Navi;
    
    // Add custom classes to all items
    Navi::withClasses(['nav-item', 'custom-class']);
    
    // Remove specific classes
    Navi::withoutClasses('old-class');
    
    // Reset to default classes
    Navi::withDefaultClasses();
  7. Retrieve menu and item data from Navi

    master

    Once a menu is built, you can access its data using several methods:

    • all() or toArray(): Returns an array of all processed menu items.
    • toJson(int $options = 0): Returns the menu items as a JSON string.
    • get(?string $key = null, mixed $default = null): Retrieves a property from the underlying WordPress menu object. If no key is provided, it returns the menu object itself.
    • isEmpty(): Returns true if the menu contains no items.
    • isNotEmpty(): Returns true if the menu contains items.
    • Dynamic Access: You can access menu object properties directly via magic __get (e.g., $navi->name).
    $navi = Navi::make()->build();
    
    // Get all items
    $items = $navi->all();
    
    // Get a specific property from the WP Menu object (e.g., the menu name)
    $menuName = $navi->name;
    
    // Check if menu has content
    if ($navi->isNotEmpty()) {
        // ...
    }
  8. Initialize and build a menu with Navi::make()

    master

    To use Navi, create an instance using the Navi::make() static method. You must call build() to fetch the menu data from WordPress. You can pass a specific menu name or slug to build(); otherwise, it defaults to primary_navigation.

    Before calling build(), you can configure which CSS classes should be stripped from the menu items using withClasses() or withoutClasses().

    use Log1x\Navi\Navi;
    
    // Initialize and build the menu
    $navi = Navi::make()->build('main-menu-slug');
    
    // Access the items
    $items = $navi->all();
  9. Filter menu item classes using withoutClasses()

    master

    You can prevent specific CSS classes from appearing on your menu items by calling withoutClasses() on the MenuBuilder instance before calling build(). This method accepts an array of strings. If a menu item's class starts with any of the provided strings, it will be removed.

    $builder = MenuBuilder::make()->withoutClasses(['wp-', 'menu-item-']);
    $menu = $builder->build($menu_items);
  10. Build a navigation menu with MenuBuilder

    master

    The MenuBuilder class is used to transform raw WordPress menu items (typically from wp_get_nav_menu_items) into a structured, hierarchical object tree.

    To use it, call MenuBuilder::make(), optionally configure class filtering via withoutClasses(), and then pass your menu array to build(). The resulting array is indexed by item ID and contains nested children arrays for each menu item.

    use Log1x\Navi\MenuBuilder;
    
    // Assuming $menu_items is the array from wp_get_nav_menu_items()
    $menu = MenuBuilder::make()
        ->withoutClasses(['some-unwanted-class'])
        ->build($menu_items);