如何将第二层子菜单添加到Walker Nav?

时间:2021-10-14 作者:RRhodes

我需要能够访问nav上子菜单的子菜单。目前我只能访问第一个子菜单层。访问这些需要添加什么?

<?php


class Walker_Nav_Primary extends Walker_Nav_Menu {

    function start_lvl( &$output, $depth = 0, $args = array() )  // ul
    {
        $indent = str_repeat("\\t", $depth);
        $submenu = ($depth > 0) ? \'sub-menu\' : \'\';
        $output .="\\n$indent<ul class=\\"submenu-first $submenu depth_$depth\\">\\n";
    }

  
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
        
        $indent = ( $depth ) ? str_repeat("\\t",$depth) : \'\';
        
        $li_attributes = \'\';
        $class_names = $value = \'\';
        
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        
        $classes[] = ($args->walker->has_children) ? \'has_sub\' : \'\';
        $classes[] = ($item->current || $item->current_item_ancestor) ? \'active\' : \'\';
        $classes[] = \'menu-item-\' . $item->ID;
        if( $depth && $args->walker->has_children ){
            $classes[] = \'dropdown-submenu\';
        }

        $class_names =  join(\' \', apply_filters(\'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
        $class_names = \' class="\' . esc_attr($class_names) . \'"\';

        $id = apply_filters(\'nav_menu_item_id\', \'menu-item-\'.$item->ID, $item, $args);
        $id = strlen( $id ) ? \' id="\' . esc_attr($id) . \'"\' : "";
        $output .= $indent . \'<li\' . $id . $value . $class_names . $li_attributes . \'>\';

        $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) . \'"\' : \'\';
        
        $attributes .= ( $args->walker->has_children ) ? \' class="has_sub_link" data-toggle="has_sub_link"\' : \'\';
        
        $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 .= ( $depth == 0 && $args->walker->has_children ) ? \' <b class="caret"></b></a>\' : \'</a>\';
        $item_output .= $args->after;
        
        $output .= apply_filters ( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
    }
}

1 个回复
最合适的回答,由SO网友:admcfajn 整理而成

无需测试(&A);调试您的示例我不能百分之百确定出了什么问题。我注意到你的助行器没有end_lvl 方法,这将是我的第一个猜测,为什么下拉超过第一级将不起作用。

这是一个导航助行器的副本,我确信它能很好地处理嵌套的导航菜单。您将要替换start_el 内容与示例中的内容一致。

<?php
class Walker_Nav_Primary extends Walker_Nav_Menu {


    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\\t", $depth);
        $output .= "\\n<ul class=\\"sub-menu\\">\\n";
    }

    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\\t", $depth);
        $output .= "</ul>\\n";
    }

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';

        $class_names = $value = \'\';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = \'menu-item-\' . $item->ID;

        $class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );

        $class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';

        $id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
        $id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';

        $output .= $indent . \'\';

        $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 .= \'<li\'. $class_names .\'><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 = 0, $args = array() ) {
        $output .= "\\n";
    }
}