小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

php – WordPress菜單:?jiǎn)螕舾覆藛雾?xiàng),僅顯示該鏈接的子導(dǎo)航子項(xiàng)

 印度阿三17 2019-08-28

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

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ù)僅引入這樣的父項(xiàng):

  <?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)建一個(gè)子導(dǎo)航,顯示這樣的子項(xiàng):

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

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

有沒有辦法改變我必須只為特定的父菜單項(xiàng)拉入子項(xiàng)的功能?任何幫助/指導(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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約