php – WordPress菜单:单击父菜单项,仅显示该链接的子导航子项

我的WordPress导航功能遇到了一些问题.我有以下功能从管理员拉取菜单项:

function cr_get_menu_items($menu_location)
{
    $locations = get_nav_menu_locations();
    $menu = get_term($locations[$menu_location], 'nav_menu');
    return wp_get_nav_menu_items($menu->term_id);
}

在我的导航模板中,我使用此函数仅引入这样的父项:

  <?php $nav = cr_get_menu_items('navigation_menu') ?>
  <?php foreach ($nav as $link):
    if ($link->menu_item_parent == 0) : ?>
    <a class="main-nav" href="<?= $link->url ?>"><?= $link->title ?></a>
  <?php endif; endforeach; ?>

我试图创建一个子导航,显示这样的子项:

<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link):
if ($link->menu_item_parent !== 0) : ?>
<a href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endif; endforeach; ?>

这将拉入所有子菜单项.我正在构建的导航应该工作的方式是:您单击父菜单项,子导航显示该父项的所有子菜单项.隐藏/显示功能都是JS.

有没有办法改变我必须只为特定的父菜单项拉入子项的功能?任何帮助/指导表示赞赏.

解决方法:

Is there a way to alter the function I have to pull in only children
for a specific parent menu item?

为此目的,是的,有.

尝试以下函数(替换现有的cr_get_menu_items()函数):

function cr_get_menu_items($menu_location, $parent = -1)
{
    $locations = get_nav_menu_locations();
    $menu = get_term($locations[$menu_location], 'nav_menu');
    $items = wp_get_nav_menu_items($menu->term_id);

    if ( is_numeric( $parent ) && $parent >= 0 ) {
        $_id = (int) $parent;
        foreach ( $items as $i => $item ) {
            if ( $_id !== (int) $item->menu_item_parent ) {
                unset( $items[ $i ] );
            }
        }
    }

    return $items;
}

用法示例:

$nav = cr_get_menu_items( 'navigation_menu' );    // Get all menu items.
$nav = cr_get_menu_items( 'navigation_menu', 0 ); // Get menu items whose parent ID is 0

UPDATE

在我重新阅读您的问题之后,这是您可能需要的功能:

// $items is the menu items array that you retrieved using `cr_get_menu_items()`,
// or other functions which return valid `nav_menu` items.
function cr_get_submenu_items( array $items, $parent ) {
    $parent = (int) $parent;

    $list = [];
    foreach ( $items as $item ) {
        if ( $parent === (int) $item->menu_item_parent ) {
            $list[] = $item;
        }
    }

    return $list;
}

更新#2

以下是cr_get_menu_items()和cr_get_submenu_items()的用法:

<?php $nav = cr_get_menu_items('navigation_menu') ?>

<!-- Display parent items. -->
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link):
if ($link->menu_item_parent == 0) : ?>
<a class="main-nav" href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endif; endforeach; ?>

<!-- Display children items. (in its own wrapper `div`/`ul`/etc.) -->
<?php $_ids = []; ?>
<?php foreach ($nav as $link):
$parent = (int) $link->menu_item_parent;
if ( 0 !== $parent && ! in_array( $parent, $_ids ) ) : ?>
<!-- This `div` is just an example wrapper. -->
<div class="menu-<?= $parent ?>-subnav">
    <?php foreach ( cr_get_submenu_items( $nav, $parent ) as $clink ): ?>
    <a href="<?= $clink->url ?>"><?= $clink->title ?></a>
    <?php endforeach; ?>
    <?php $_ids[] = $link->menu_item_parent; ?>
</div>
<?php endif; endforeach; ?>
上一篇:022 Jquery总结


下一篇:python – 使用SoftBank库的Pepper制图和导航