I’m trying to modify the nav walker menu in order to have a mega menu on every li with ‘has-mega-menu’ class and the default menu on every other li
class Mega_Menu extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$location = 'mega_menu';
$css_class = 'has-mega-menu';
$locations = get_nav_menu_locations();
$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names .'>';
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
if ( is_array($item->classes) && in_array( $css_class, $item->classes) ){
if ( isset( $locations[ 'mega_menu' ] ) ) {
$menu = get_term( $locations[ 'mega_menu' ], 'nav_menu' );
if ( $items = wp_get_nav_menu_items( $menu->name ) ) {
foreach ( $items as $item ) {
$output .= "<a href="{$item->url}">{$item->title}</a>";
if ( is_active_sidebar( 'mega-menu-widget-area-' . $item->ID ) ) {
$output .= "<div id="mega-menu-{$item->ID}" class="mega-menu">";
dynamic_sidebar( 'mega-menu-widget-area-' . $item->ID );
$output .="</div>";
}
}
}
}
}
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $item->title, $item->ID );
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>n";
}
}
the problem is that i have in my mega menu all the item of my menu
Ok, so I remember almost sorting this issue before, so I revisited it and I solved it. I hope it will help anybody who reads this.
The gist can be found here: click to gist.
First your menu walker. I’ve added a walker that has sidebars in the menu and megamenu, so that you can even put your sidebars (widgets) in the megamenu if you want (neat trick :D)
I won’t go into the details about what’s what (I’ll leave that for my site later on), but just trust me, it works 😀
Also you need a bit of JS help with the
change
events and options staying selected. Add this in a separate .js file that you’ll include only onnav-menus.php
pageThis will give you something like this:
Now the ‘only’ thing I didn’t include here is the css. But you can figure it out, not that hard. Basically the key thing is this:
This will set the widths of your
li
s inside the megamenu menu. You’ll also need to set them either asfloat:left
(easy way), or asinline-block
elements withvertical-align:top
and afont-size:0
on the parentul
element (so that they fit).Hope this helps out with the megamenu problem 😀