2

我有一个 3 级导航:

Home
   > submenu1
       >> sub3       
   > submenu
       >> sub4       
       >> sub5       // current page
About
   > about2
       >> sub6       
   > about3
       >> sub7       

我正在尝试分别获取每个导航级别,

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

当前一级导航:首页

当前并行2级nav: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 级导航。

谢谢。

当前代码:第二个导航作品:

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

    // Convert Objects to Arrays, Enables us to use Array Filter
    $json  = json_encode($menu_items);
    $menu_items = json_decode($json, true);

    // Current Page
    $child = get_the_id();


    $current_level = array_filter( $menu_items, function($v, $k) use ($child) {
    return $v['object_id'] == $child;
    }, ARRAY_FILTER_USE_BOTH );

    $current_level_keys = array_keys($current_level);
    $parent = $current_level[$current_level_keys[0]]['menu_item_parent'];

    if( !empty( $parent ) )
    {
    $current_level_items = array_filter( $menu_items, function($v, $k) use ($parent)  {
        return $v['menu_item_parent'] == $parent;
    }, ARRAY_FILTER_USE_BOTH );
    } else {
    $current_level_items = $current_level[$current_level_keys[0]];
    }

    //echo '1:';
    //echo '<pre>';
    //print_r($current_level_items);
    //echo '</pre>';

    //foreach ($current_level_items as $k => $v) {
    //  echo '<li><a href="#">'.$v['title'].'</a></li>';
    //}

    $parent_level = array_filter( $menu_items, function($v, $k) use ($parent) {
    return $v['ID'] == $parent;
    }, ARRAY_FILTER_USE_BOTH );

    $parent_level_keys = array_keys($parent_level);
    $grand_parent = $parent_level[$parent_level_keys[0]]['menu_item_parent'];

    if( !empty( $grand_parent ) )
    {
    $parent_level_items = array_filter( $menu_items, function($v, $k) use ($grand_parent)  {
        return $v['menu_item_parent'] == $grand_parent;
    }, ARRAY_FILTER_USE_BOTH );
    } else {
    $parent_level_items = $parent_level[$parent_level_keys[0]];
    }

    //echo '2:';
    //echo '<pre>';
    //print_r($parent_level_items);
    //echo '</pre>';
    //foreach ($parent_level_items as $k => $v) {
    //echo '<li><a href="#">'.$v['title'].'</a></li>';
    //}

    $grand_parent_level = array_filter( $menu_items, function($v, $k) use ($grand_parent) {
    return $v['ID'] == $grand_parent;
    }, ARRAY_FILTER_USE_BOTH );

    $grand_parent_level_keys = array_keys($grand_parent_level);
    $great_grand_parent = $grand_parent_level[$grand_parent_level_keys[0]];

    if( !empty( $parent ) ) {
        if( !empty( $great_grand_parent ) ) {
        echo '<li class="custom-page-title">'.$great_grand_parent['title'].'</li>';
        if (is_array($parent_level_items)) {
            foreach ($parent_level_items as $k => $v) {
                echo '<li><a href="'.$v['url'].'">'.$v['title'].'</a></li>';
            }
        }
        } else {
        echo '<li class="custom-page-title">'.$parent_level_items['title'].'</li>';
        if (is_array($current_level_items)) {
            foreach ($current_level_items as $k => $v) {
                echo '<li><a href="'.$v['url'].'">'.$v['title'].'</a></li>';
            }
        }
        }
    }
    //echo '3:';
    //echo '<pre>';
    //print_r($great_grand_parent);
    //echo '</pre>';

}
?>

但是,如果菜单中有重复的(第 3 级)页面,则此脚本仅采用第一个父级......它可能是最后一个或真正的父级(可能来自 url 路径?)

菜单问题中的重复页面:

Home
   > submenu1
       >> sub3
   > submenu
       >> sub4       
       >> sub5       // current page
About
   > about2
       >> sub6       
   > about3
       >> sub7
       >> sub5       // duplicate page

访问重复页面(sub5)时,返回的父级是第一个(Home > submenu),应该是(About > about3)

请帮助解决这个问题...

4

1 回答 1

2

编辑:

要解决重复的情况,您必须添加一行代码。您可以根据要选择的父链添加以下行之一。我还在实际代码中添加了这些行,以向您展示它们的去向。

// 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 ] ) ) {

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

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

    // Convert Objects to Arrays, Enables us to use Array Filter
    $json  = json_encode($menu_items);
    $menu_items = json_decode($json, true);

    // Current Page
    $child = get_the_id();


    $current_level = array_filter( $menu_items, function($v, $k) use ($child) {
        return $v['object_id'] == $child;
    }, ARRAY_FILTER_USE_BOTH );

    // Get First Parent Chain ( Uncomment below line if you want to use this )
    //$current_level = array_values(array_slice($current_level, 0, 1));

    // Get Last Parent Chain ( Uncomment below line if you want to use this )
    //$current_level = array_values(array_slice($current_level, -1, 1));


    $current_level_keys = array_keys($current_level);
    $parent = $current_level[$current_level_keys[0]]['menu_item_parent'];

    if( !empty( $parent ) )
    {
        $current_level_items = array_filter( $menu_items, function($v, $k) use ($parent)  {
            return $v['menu_item_parent'] == $parent;
        }, ARRAY_FILTER_USE_BOTH );
    } else {
        $current_level_items = $current_level[$current_level_keys[0]];
    }

    echo '<pre>';
    print_r($current_level_items);
    echo '</pre>';

    $parent_level = array_filter( $menu_items, function($v, $k) use ($parent) {
        return $v['ID'] == $parent;
    }, ARRAY_FILTER_USE_BOTH );

    $parent_level_keys = array_keys($parent_level);
    $grand_parent = $parent_level[$parent_level_keys[0]]['menu_item_parent'];

    if( !empty( $grand_parent ) )
    {
        $parent_level_items = array_filter( $menu_items, function($v, $k) use ($grand_parent)  {
            return $v['menu_item_parent'] == $grand_parent;
        }, ARRAY_FILTER_USE_BOTH );
    } else {
        $parent_level_items = $parent_level[$parent_level_keys[0]];
    }

    echo '<pre>';
    print_r($parent_level_items);
    echo '</pre>';

    $grand_parent_level = array_filter( $menu_items, function($v, $k) use ($grand_parent) {
        return $v['ID'] == $grand_parent;
    }, ARRAY_FILTER_USE_BOTH );

    $grand_parent_level_keys = array_keys($grand_parent_level);
    $great_grand_parent = $grand_parent_level[$grand_parent_level_keys[0]];

    echo '<pre>';
    print_r($great_grand_parent);
    echo '</pre>';

}

我试图把它写成不言自明的,但如果你有任何问题,我很乐意提供帮助。

于 2019-08-19T09:56:44.517 回答