【问题标题】:Display a list of sub-pages on the current page based on the WP menu根据 WP 菜单在当前页面显示子页面列表
【发布时间】:2019-05-01 03:12:37
【问题描述】:

我在外观 > 菜单下创建了一个名为“侧边栏菜单集合”的菜单

这是他们的等级制度

Material
- marble
- onyx
- slate
- granite

Applications
- benchtops
- floors
- walls

products
- Slab
- pavers
- cladding

因此,当我在“材料”页面上时,我只想列出其子菜单(大理石、缟玛瑙、石板、格兰特石)。

如果我在“应用程序”页面中,它会显示(台面、地板、墙壁)。依此类推,我想根据页面特色图片在每个子列表中添加特色图片。

这也仅适用于父菜单(材料、应用程序、产品),如果您在子菜单上,则不会显示任何列表

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您好,使用下面的代码只会显示特定页面的子页面

    wp_list_pages( $args = '' ) {
        $defaults = array(
            'depth'        => 1,
            'show_date'    => '',
            'date_format'  => get_option( 'date_format' ),
            'child_of'     => $post->ID,
            'exclude'      => '',
            'title_li'     => __( 'Pages' ),
            'echo'         => 1,
            'authors'      => '',
            'sort_column'  => 'menu_order, post_title',
            'link_before'  => '',
            'link_after'   => '',
            'item_spacing' => 'preserve',
            'walker'       => '',
        );  
    

    【讨论】:

    • 他们没有父母,只是基于菜单结构,所以像菜单祖先什么的
    【解决方案2】:

    已经有了答案,来自post

    这是完美的工作,我现在唯一的问题是如何在每个链接上添加特色图片。

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

    用法

    wp_nav_menu( array(
      'menu'     => 'Menu Name',
      'sub_menu' => true
    ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      相关资源
      最近更新 更多