Why is the Walker_Nav_Menu self closing tags added in start_lvl

I’m creating a custom Walker based on Walker_Nav_Menu for a megamenu I’m working on and the issue I’m having is that it seems the tags I open on start_lvl are being automatically closed. Here’s my code:

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

            // Add specific tags only on first submenu
    if (0 == $depth) {

        $output .= "n$indent<li>n";
        $output .= "n$indent<div class='yamm-content'>n";

    }
}

And on my end_lvl I’ve got this:

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

    $indent = str_repeat("t", $depth);
    $output .= "$indent</ul>n";

    if (0 == $depth) {

        $output .= "$indent</div>n";
        $output .= "$indent</li>n";

    }
}

However with or without end_lvl I get this output:

<li>
    <div class="yamm-content"> </div>
</li>
<li>Menu item 1<li>
<li>Menu item 2<li> // These items should be inside the above div
...

I’m new to Walker but I thought that whatever I add to start_lvl would wrap the

  • tags. What am I missing?

    PS: I have tried without the conditional if (0 == $depth) and the difference was only the above code was in every level rather than just on the first.

    Read more here: Why is the Walker_Nav_Menu self closing tags added in start_lvl

  • Leave a Reply

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