【问题标题】:Wordpress Get top Level Menu Item TitleWordpress 获取顶级菜单项标题
【发布时间】:2017-06-08 02:17:17
【问题描述】:

我正在开发一个 WP 主题,我想要的是当前顶级项目侧边栏上的导航。

  • 菜单项
    • 子菜单项
      • 子菜单项
      • 子菜单项
      • 子菜单项
    • 子菜单项
      • 子菜单项
      • 子菜单项
      • 子菜单项
    • 子菜单项
      • 子菜单项
      • 子菜单项
      • 子菜单项

下面的代码在侧边栏上可见,除了“菜单项(顶部父项)”。你知道如何做到这一点吗?

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

有 回声 $root_id; 我能够获取根菜单项 ID,但我想显示标题。

这是我在侧边栏中的代码

<section class="widget widget_navbar">
  <?php
  if (has_nav_menu('primary_navigation')) :
    wp_nav_menu([
        'theme_location' => 'primary_navigation', 
         'sub_menu'       => true,

    ]);
  endif;
  ?>

</section>

【问题讨论】:

    标签: html wordpress menu navigation


    【解决方案1】:

    这个过滤器的作者——Christian Varga——在这里有说明:https://gist.github.com/levymetal/5547605#file-show_parent-php。原博文在这里:https://christianvarga.com/how-to-get-submenu-items-from-a-wordpress-menu-based-on-parent-or-sibling/

    wp_nav_menu( array(
      'theme_location' => 'primary',
      'sub_menu'       => true,
      'show_parent'    => true // this shows the parent
    ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多