微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将Walker Nav菜单拆分为4列

如何解决将Walker Nav菜单拆分为4列

我正在尝试将自定义超级菜单的子菜单部分分为四列。我几乎在下面的代码中使用该代码,但是,这只会将这些列分为两部分。

我似乎已经尝试了所有变体,但显然缺少一些东西。有帮助吗?

class megamenu_panel extends Walker_Nav_Menu {

    var $current_menu = null;
    var $break_point  = null;

    function start_lvl( &$output,$depth = 0,$args = array() ) {
        $indent = str_repeat("\t",$depth);
        $output .= "\n$indent<div class='megamenu-panel'><div class='megamenu-lists'><ul class='megamenu-list list-col-4'>\n";
    }
    function end_lvl( &$output,$depth);
        $output .= "$indent</ul></div></div>\n";
    }

    function start_el(&$output,$item,$depth,$args) {

        global $wp_query;

        if( !isset( $this->current_menu ) )
            $this->current_menu = wp_get_nav_menu_object( $args->menu );

        if( !isset( $this->break_point ) )
            $this->break_point = ceil( $this->current_menu->count / 4 ) + 1;    

        $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 ),$args ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';

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

        if( $this->break_point == $item->menu_order )
            $output .= $indent . '</ul><ul class="megamenu-list list-col-4"><li' . $id . $value . $class_names .'>';
        else
            $output .= $indent . '<li' . $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,$args );
    }
}

任何帮助将不胜感激。

非常感谢, SW

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。