1

I'm trying to create a normal dropdown menu and a megamenu in WP by using a custom walker.

The problem that I have is that the usual ul tag needs to be replaced with a div.

I already created the an extra checkbox to enable the megamenu by using the wp_update_nav_menu_item and wp_setup_nav_menu_item filters.

function custom_menu_fields( $menu_item ) { 
    $menu_item->is_mega_menu = get_post_meta( $menu_item->ID, '_menu_item_megamenu', true );
    return $menu_item;
}
add_filter('wp_setup_nav_menu_item', 'custom_menu_fields');


function fields_update($menu_id, $menu_item_db_id, $args) {         
    if ( isset($_REQUEST['menu-item-megamenu']) && is_array($_REQUEST['menu-item-megamenu'])) {
        $custom_value = $_REQUEST['menu-item-megamenu'][$menu_item_db_id];
        update_post_meta($menu_item_db_id, '_menu_item_megamenu', $custom_value);
    }   
}
add_action('wp_update_nav_menu_item', 'fields_update', 10, 3);

Then my walker:

public function start_lvl( &$output, $depth = 0, $args = array() ) {

        $indent     = str_repeat("\t", $depth);     

        if( $this->is_mega_menu && $depth == 0 ) { 
             $indent = "\t$indent";
             $output.= "$indent <div class=\"mega-menu\">\n";
        } else {
            $output.= "\n$indent<ul class=\"dropdown-menu\">\n";
        }   

The problem is $this->is_mega_menu. Normally I would use $item->is_mega_menu or $item->classes but both won't work inside the start_lvl public funtion due to the "...should be compatible with Walker::start_lvl(&$output, $depth = 0, $args = Array)" the moment I add $item.

How do I connect $this->is_mega_menu with the custom checkbox to enable the megamenu? Any help is much appreciated.

EDIT

I used var_dump($args); to see what options I have and I had to changed $this->is_mega_menu to $args->walker->is_mega_menu

There's still one thing left; is_mega_menu remains NULL

object(stdClass)#1687 (17) {    
    ....
    ["depth"]=> int(0) 

    ["walker"]=> object(Custom_Nav_Menu)#1688 (5) { 
        ["db_fields"]=> array(2) { ["parent"]=> string(16) "menu_item_parent" ["id"]=> string(5) "db_id" } 
        ["is_mega_menu"]=> NULL         
        ["has_children"]=> bool(true) 
        }   
    "theme_location"]=> string(15) "main_navigation"    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.