WordPress, wrap a sub menu in a div if the parent li has a specific class, using a walker and the start_lvl function

I am trying to make a custom nav menu using a walker, that has a mega menu. What I am trying to achieve is when someone adds a class of “has-megamenu” to a nav item in the dashboard, the sub-menu gets wrapped in a div with a class of “is-megamenu”.

I imagined I could detect the class from the start_lvl function and add the markup there, something like this:

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

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

    if (in_array("has-megamenu", $classes)) {
        $output .= "n$indent<div class="is-megamenu"><ul class="is-submenu">n";
    }else{
         $output .= "n$indent<ul class="is-submenu depth_$depth">n";
    }

}

This doesn’t work as it appears the class names of the parent level of the nav are not available at this point.

I am new to PHP and WordPress so I am at a loss of how to achieve this.

Read more here: WordPress, wrap a sub menu in a div if the parent li has a specific class, using a walker and the start_lvl function

Leave a Reply

Your email address will not be published. Required fields are marked *