免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
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; ?>

我試圖創(chuà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; ?>

這將拉入所有子菜單項.我正在構建的導航應該工作的方式是:您單擊父菜單項,子導航顯示該父項的所有子菜單項.隱藏/顯示功能都是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; ?>

來源:https://www.icode9.com/content-1-417901.html
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
NavigationView 的使用
ecshop簡單三步實現導航商品分類二級菜單
WordPress主題制作導航的N種方法
欣馨心QQ好友?芯?╰︽原創(chuàng)制作空間︽╯
【Fluent GUI】08:菜單
wordpress之wp_nav_menu使用說明
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服