仅显示当前父级及其子菜单 - wordpress

var*_*run 5 php wordpress

我有一个主导航,所有父母都有孩子.例如:

Page A: About Us
child1
child2

Page B : Our services
Child 3
Child 4
Run Code Online (Sandbox Code Playgroud)

我需要在页面上包含一个水平子菜单.但我的问题是,如果目前我们在页面A上,页面A的所有子项目只显示在页面上.如果我们在页面A上,它应该看起来像:

Page A
Child 1
Child 2

像这样,当我们转到页面B时,页面B的孩子才会被显示.

      <?php 
  $args = array(
   'theme_location'  => '',
   'menu'            => '13',  //nav menu id, which has about-us as a menu.
   'container'       => 'div',
   'container_class' => '',
   'container_id'    => '',
   'menu_class'      => 'menu',
   'menu_id'         => '',
   'echo'            => true,
   'fallback_cb'     => 'wp_page_menu',
   'before'          => '',
   'after'           => '',
   'link_before'     => '',
   'link_after'      => '',
   'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
   'depth'           => 0,
   'walker'          => ''
);
  $menu_items = wp_nav_menu($args);//wp_get_nav_menu_items(13);
Run Code Online (Sandbox Code Playgroud)

我尝试编写上面的代码,这导致所有父项与他们的孩子.

有人可以帮我吗?

简而言之,我想得到关于我们菜单项的所有孩子(子菜单).(即我希望child1和child2作为带有<a>标签的列表)

Ati*_*riq 5

请在主题的functions.php中编写此代码

<?php

// 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;
}
}
Run Code Online (Sandbox Code Playgroud)

然后您可以使用 wp_nav_menu 将其显示在您的主题中(就像您通常会那样),但也可以传入 sub_menu 标志以激活自定义 sub_menu 功能:

<?php
wp_nav_menu( array(
  'theme_location' => 'primary',
  'sub_menu' => true
) );
?>
Run Code Online (Sandbox Code Playgroud)


var*_*run 1

这就是全部

<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div>
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

感谢所有的解决方案!