Wordpress 中的当前级别导航

我有一个 3 级导航:


Home

   > submenu1

       >> sub3       

   > submenu

       >> sub4       

       >> sub5       // current page

About

   > about2

       >> sub6       

   > about3

       >> sub7       

我试图分别获得每个导航级别,


使用上面的示例导航,在 sub5 页面上,我需要


当前第一级导航:首页


当前并行的第二级导航:submenu1、submenu(都在 Home 下)


当前并行的第 3 级导航:sub4、sub5(子菜单下的菜单)


我需要修改菜单的元素和样式,并且需要使用 php 中的菜单项,例如:


$menu_name = 'topnav';

if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {

$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

$menu_items = wp_get_nav_menu_items($menu->term_id);


foreach ( (array) $menu_items as $key => $menu_item ) {

    $title = $menu_item->title;

    $url = $menu_item->url;

    [...]

请帮助分别获取导航层以按上述说明显示。


我让第 1 级和第 3 级工作正常,但无法正确显示当前并行的第 2 级导航。


谢谢。


qq_遁去的一_1
浏览 168回答 1
1回答

互换的青春

编辑:要解决重复的情况,您必须添加一行代码。您可以根据要选择的父链添加以下行之一。我还在实际代码中添加了这些行,以向您展示它们的去向。// Get First Parent Chain$current_level = array_values(array_slice($current_level, 0, 1));// Get Last Parent Chain$current_level = array_values(array_slice($current_level, -1, 1));原始答案:(也更新了以上几行)此代码将为您提供所有级别的项目,它仅根据您的要求编写了三个级别,但您可以使用该逻辑并根据需要将代码重复到多个级别,或者最好编写一些递归的内容。$menu_name = 'topnav';if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {&nbsp; &nbsp; $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );&nbsp; &nbsp; $menu_items = wp_get_nav_menu_items( $menu->term_id );&nbsp; &nbsp; // Convert Objects to Arrays, Enables us to use Array Filter&nbsp; &nbsp; $json&nbsp; = json_encode($menu_items);&nbsp; &nbsp; $menu_items = json_decode($json, true);&nbsp; &nbsp; // Current Page&nbsp; &nbsp; $child = get_the_id();&nbsp; &nbsp; $current_level = array_filter( $menu_items, function($v, $k) use ($child) {&nbsp; &nbsp; &nbsp; &nbsp; return $v['object_id'] == $child;&nbsp; &nbsp; }, ARRAY_FILTER_USE_BOTH );&nbsp; &nbsp; // Get First Parent Chain ( Uncomment below line if you want to use this )&nbsp; &nbsp; //$current_level = array_values(array_slice($current_level, 0, 1));&nbsp; &nbsp; // Get Last Parent Chain ( Uncomment below line if you want to use this )&nbsp; &nbsp; //$current_level = array_values(array_slice($current_level, -1, 1));&nbsp; &nbsp; $current_level_keys = array_keys($current_level);&nbsp; &nbsp; $parent = $current_level[$current_level_keys[0]]['menu_item_parent'];&nbsp; &nbsp; if( !empty( $parent ) )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $current_level_items = array_filter( $menu_items, function($v, $k) use ($parent)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $v['menu_item_parent'] == $parent;&nbsp; &nbsp; &nbsp; &nbsp; }, ARRAY_FILTER_USE_BOTH );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $current_level_items = $current_level[$current_level_keys[0]];&nbsp; &nbsp; }&nbsp; &nbsp; echo '<pre>';&nbsp; &nbsp; print_r($current_level_items);&nbsp; &nbsp; echo '</pre>';&nbsp; &nbsp; $parent_level = array_filter( $menu_items, function($v, $k) use ($parent) {&nbsp; &nbsp; &nbsp; &nbsp; return $v['ID'] == $parent;&nbsp; &nbsp; }, ARRAY_FILTER_USE_BOTH );&nbsp; &nbsp; $parent_level_keys = array_keys($parent_level);&nbsp; &nbsp; $grand_parent = $parent_level[$parent_level_keys[0]]['menu_item_parent'];&nbsp; &nbsp; if( !empty( $grand_parent ) )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $parent_level_items = array_filter( $menu_items, function($v, $k) use ($grand_parent)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $v['menu_item_parent'] == $grand_parent;&nbsp; &nbsp; &nbsp; &nbsp; }, ARRAY_FILTER_USE_BOTH );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $parent_level_items = $parent_level[$parent_level_keys[0]];&nbsp; &nbsp; }&nbsp; &nbsp; echo '<pre>';&nbsp; &nbsp; print_r($parent_level_items);&nbsp; &nbsp; echo '</pre>';&nbsp; &nbsp; $grand_parent_level = array_filter( $menu_items, function($v, $k) use ($grand_parent) {&nbsp; &nbsp; &nbsp; &nbsp; return $v['ID'] == $grand_parent;&nbsp; &nbsp; }, ARRAY_FILTER_USE_BOTH );&nbsp; &nbsp; $grand_parent_level_keys = array_keys($grand_parent_level);&nbsp; &nbsp; $great_grand_parent = $grand_parent_level[$grand_parent_level_keys[0]];&nbsp; &nbsp; echo '<pre>';&nbsp; &nbsp; print_r($great_grand_parent);&nbsp; &nbsp; echo '</pre>';}我试图将其写成自我解释,但如果您有任何问题,我将很乐意提供帮助。
打开App,查看更多内容
随时随地看视频慕课网APP