日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

php – WordPress菜單:單擊父菜單項,僅顯示該鏈接的子導(dǎo)航子項

 印度阿三17 2019-08-28

我的WordPress導(dǎo)航功能遇到了一些問題.我有以下功能從管理員拉取菜單項:

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);
}

在我的導(dǎo)航模板中,我使用此函數(shù)僅引入這樣的父項:

  <?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; ?>

我試圖創(chuàng)建一個子導(dǎo)航,顯示這樣的子項:

<?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; ?>

這將拉入所有子菜單項.我正在構(gòu)建的導(dǎo)航應(yīng)該工作的方式是:您單擊父菜單項,子導(dǎo)航顯示該父項的所有子菜單項.隱藏/顯示功能都是JS.

有沒有辦法改變我必須只為特定的父菜單項拉入子項的功能?任何幫助/指導(dǎo)表示贊賞.

解決方法:

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

為此目的,是的,有.

嘗試以下函數(shù)(替換現(xiàn)有的cr_get_menu_items()函數(shù)):

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; ?>

來源:https://www./content-1-417901.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約