wordpress菜单仅在父页面上显示子项

问题描述 投票:-1回答:1

我有以下菜单结构:

- Page 1
-- Subpage 1 of Page 1
-- Subpage 2 of Page 1
-- Subpage 3 of Page 1
- Page 2
- Page 3
- Page 4

现在我想生成一个带有wp_nav_menu函数的菜单,该函数默认显示父页面。只有当您在包含子页面和子页面的页面上时,才应显示子页面(子项)。

这意味着当我在第1页上时,菜单应如上所示。当您在第1页的第1页或第1页的子页2或第1页的第3页时,菜单也必须如下所示。

但是当我在第4页(或第2页或第3页)上时,菜单应如下所示:

- Page 1
- Page 2
- Page 3
- Page 4

我怎样才能做到这一点?

wordpress
1个回答
0
投票

你应该编写自定义的WalkerMenu类:

class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {
    public $found_parents = array();
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;

        //this only works for second level sub navigations
        $parent_item_id = 0;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';


        if ( ($item->menu_item_parent==0) && (strpos($class_names, 'current-menu-parent')) ) {
            $output.= '
<li>';
        }
        // Checks if the current element is in the current selection
        if (strpos($class_names, 'current-menu-item')
            || strpos($class_names, 'current-menu-parent')
            || strpos($class_names, 'current-menu-ancestor')
            || (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) {
            // Keep track of all selected parents
            $this->found_parents[] = $item->ID;
            //check if the item_parent matches the current item_parent
            if($item->menu_item_parent!=$parent_item_id){
                $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
                $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
                $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
                $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
                $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
                $item_output = $args->before;
                $item_output .= '<a'. $attributes .'>';
                $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
                $item_output .= '</a>';
                $item_output .= $args->after;

                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
            }


        }
    }

    function end_el(&$output, $item, $depth) {
        // Closes only the opened li
        if ( is_array($this->found_parents) && in_array( $item->ID, $this->found_parents ) ) {
            $output .= "</li>\n";
        }
    }
    function end_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        // If the sub-menu is empty, strip the opening tag, else closes it
        if (substr($output, -22)=="<ul class=\"sub-menu\">\n") {
            $output = substr($output, 0, strlen($output)-23);
        } else {
            $output .= "$indent</ul>\n";
        }
    }

}

wp_nav_menu(
    array(
        'theme_location'  => 'sidebar-menu', //assign nav in admin
        'menu'            => '',
        'container'       => 'div',
        'container_class' => 'menu-{menu slug}-container',
        'container_id'    => '',
        'menu_class'      => 'menu',
        'menu_id'         => '',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'before'          => '',
        'after'           => '',
        'link_before'     => '',
        'link_after'      => '',
        'items_wrap'      => '<ul>%3$s</ul>',
        'depth'           => 0,
        'walker'          => new Custom_Walker_Nav_Sub_Menu()
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.