log1x/navi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

log1x/navi

Composer 安装命令:

composer require log1x/navi

包简介

A developer-friendly alternative to the WordPress NavWalker.

README 文档

README

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

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

use Log1x\Navi\Navi;

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

By default, build() uses primary_navigation if no menu location is specified.

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

if ($menu->isNotEmpty()) {
    return $menu->all();
}

Note

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

Menu Item Classes

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:

$menu = Navi::make()->withDefaultClasses()->build();

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.

$menu = Navi::make()->withoutClasses(['shop-'])->build();

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() allowing you to access it directly.

$menu->get()->name;

Optionally, you may pass a key and default to call a specific object key with a fallback when the value is blank:

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

Below is an example of getting the post type of the current menu item:

$type = 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.

$label = get_field('custom_menu_label', $item->id) ?: $item->label;

Acorn Usage

If you are using Navi alongside Acorn (e.g. Sage), you may generate a usable view component using Acorn's CLI:

$ wp acorn navi:make Menu

Once generated, you may use the view component in an existing view like so:

<x-menu name="footer_navigation" />

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

$ wp acorn navi:list

Example Output

When calling build(), Navi will retrieve the WordPress navigation menu assigned to the passed location and build out an array containing the menu items.

An example of the menu output can be seen below:

array [
  5 => {
    +"active": true
    +"activeAncestor": false
    +"activeParent": false
    +"classes": "example"
    +"dbId": 5
    +"description": false
    +"id": 5
    +"label": "Home"
    +"object": "page"
    +"objectId": "99"
    +"parent": false
    +"slug": "home"
    +"target": "_blank"
    +"title": false
    +"type": "post_type"
    +"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"
    +"object": "page"
    +"objectId": "100"
    +"parent": false
    +"slug": "sample-page"
    +"target": false
    +"title": false
    +"type": "post_type"
    +"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"
        +"object": "custom"
        +"objectId": "101"
        +"parent": 6
        +"slug": "example"
        +"target": false
        +"title": false
        +"type": "custom"
        +"url": "#"
        +"xfn": false
        +"order": 3
        +"parentObjectId": 100
        +"children": array [
          ...
        ]
      }
    ]
  }
]

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.

log1x/navi 适用场景与选型建议

log1x/navi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 691.76k 次下载、GitHub Stars 达 370, 最近一次更新时间为 2019 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「wordpress-plugin」 「navwalker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 log1x/navi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 log1x/navi 我们能提供哪些服务?
定制开发 / 二次开发

基于 log1x/navi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 691.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 370
  • 点击次数: 32
  • 依赖项目数: 8
  • 推荐数: 1

GitHub 信息

  • Stars: 370
  • Watchers: 12
  • Forks: 30
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-21