首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅显示3级Wordpress菜单

仅显示3级Wordpress菜单
EN

Stack Overflow用户
提问于 2017-01-23 22:06:24
回答 1查看 1.6K关注 0票数 0

我在这个网站上工作:http://new.leicesterymca.co.uk/youth-community/our-work/

在这个页面上,你会看到我在顶部有1级菜单,然后我有PHP代码,如果有2级菜单,则在蓝色条中显示2级菜单。

但现在我需要相同的第三级菜单(在页面的左侧)。所以基本上我需要代码来查看是否有任何第三级页面,如果有,那么在这里显示第三级页面。我有的第二级代码是:

代码语言:javascript
复制
<?php
  $menu = wp_nav_menu(
      array (
        'theme_location' => 'header-menu',
        'sub_menu' => true,
        'echo' => FALSE,
        'fallback_cb' => '__return_false'
      )
  );
  if ( ! empty ( $menu ) )
  {
    echo '<div class="sub-nav-container-full">
          <div class="container">
          <div class="sub-page-menu">';
    echo $menu;
    echo '</div></div></div>';
  }
?>

所以,我假设我可以使用类似的代码,但我不知道如何只针对第三层?

如果有人能帮忙,那就太好了!:)

谢谢,肖恩。

EXTRA -我的函数文件中也有这段代码,如果它有帮助的话…

代码语言:javascript
复制
// FUNCTION FOR SETTING UP SUB MENU PAGES

// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );

// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
  if ( isset( $args->sub_menu ) ) {
    $root_id = 0;

// find the current menu item
foreach ( $sorted_menu_items as $menu_item ) {
  if ( $menu_item->current ) {
    // set the root id based on whether the current menu item has a parent or not
    $root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
    break;
  }
}

// find the top level parent
if ( ! isset( $args->direct_parent ) ) {
  $prev_root_id = $root_id;
  while ( $prev_root_id != 0 ) {
    foreach ( $sorted_menu_items as $menu_item ) {
      if ( $menu_item->ID == $prev_root_id ) {
        $prev_root_id = $menu_item->menu_item_parent;
        // don't set the root_id to 0 if we've reached the top of the menu
        if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
        break;
      } 
    }
  }
}

$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
  // init menu_item_parents
  if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;

  if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
    // part of sub-tree: keep!
    $menu_item_parents[] = $item->ID;
  } else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
    // not part of sub-tree: away with it!
    unset( $sorted_menu_items[$key] );
  }
}

return $sorted_menu_items;
  } else {
return $sorted_menu_items;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2017-01-23 22:21:25

看起来你的问题和这个问题很相似,我认为你也可以让这个问题为你工作!

https://wordpress.stackexchange.com/questions/45161/3-level-deep-navigation-menu-not-showing-all-levels

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41808236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档