Wordpress 中的当前级别导航



我有一个 3 级导航:

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

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

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

当前一级导航:主页

当前并行的第二级导航:子菜单1,子菜单(都在主页下(

当前并行的第三级导航: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 级工作,但无法让当前并行的第二级导航正确显示。

谢谢。

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

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

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

菜单中的重复页面问题:

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

当访问重复页面(sub5(时,返回的父页面是第一个(主页>子菜单(,而它应该是(关于>关于3(

请帮助解决此问题...

编辑:

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

// 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>';
}

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

相关内容

  • 没有找到相关文章

最新更新