如何根据wordpress中的菜单结构列出当前页面/帖子的兄弟姐妹

时间:2015-03-25 04:41:41

标签: php wordpress

我一直在争论这个菜单问题一段时间,似乎无法得到我想要的答案。我想只在侧栏上显示所有“兄弟”菜单项。如果我有以下菜单结构; - 1 - 1.1 - 1.2 - 1.3 - 1.4 - 1.5 - 2 - 2.1 ....... - 2.5 - 3

如果用户在页面/帖子1.3上,我想列出

1.1

1.2

1.3

1.4

1.5

..只。目前,我能够列出具有我的功能的兄弟姐妹,但前提是我硬编码了1.3的祖先的页面1的标题。我想动态地获得1.3的祖先的名称,而不必硬编码。我怎么能;

  1. 动态获取我所在页面的直接祖先?
  2. 2.如果问题1不可能,我怎么才能得到兄弟姐妹?

    以下是我的代码;

    1. 此代码转到wordpress中的functions.php;

      <?php
             add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
      
             function submenu_limit( $items, $args ) {
      
                  if ( empty($args->submenu) )
                      return $items;
                  $arguments = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' );
                  $parent_id = array_pop( $arguments  );
                  $children  = submenu_get_children_ids( $parent_id, $items );
      
                  foreach ( $items as $key => $item ) {
      
                      if ( ! in_array( $item->ID, $children ) )
                          unset($items[$key]);
                  }
      
                 return $items;
              }
      
            function submenu_get_children_ids( $id, $items ) {
      
                $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
      
            foreach ( $ids as $id ) {
      
            $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
             }
      
            return $ids;
           }
         ?>
      

      然后下面是我用来获取子菜单的函数。正如您所看到的,我已将当前帖子的父/祖先('Business&amp; Commercial')硬编码为名为“Topest Menu”的菜单;

      <?php  
          //code that goes to the sidebar.php
          $args = array(
              'menu'    => 'Topest menu',
              'submenu' => 'Business & Commercial',
          );
      
          wp_nav_menu( $args );
      

      &GT;

0 个答案:

没有答案