I'm trying to wrap the first sub-menu of a navigation into a seperate div. None of the children or sub-levels should get the same treatment.
I'm really new to the Nav Walker-thingie and I'm really struggling and could need some help 😜
This is the structure I'm trying to achieve:
<ul class="menu">
<li class="menu-item">
<a href="#">Menu Item</a>
<!-- wrapper-class around the first sub-menu -->
<div class="sub-menu__wrapper">
<ul class="sub-menu">
<li class="menu-item">
<a href="#">Menu Item</a>
<!-- NO wrapper-class around following levels -->
<ul class="sub-menu">
<li>...</li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
I thought it would be easy to do with a simple if-statement that only prints the sub-menu__wrapper on the first level, but somehow the following HTML-output gets mixed up pretty bad.
Here's my Walker-class:
class sublevel_wrapper extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
if ($depth == 0) {
$output .= "<div class='sub-menu__wrapper'><ul class='sub-menu'>\n";
}
//$output .= "<ul class='sub-menu'>\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
if ($depth == 0) {
$output .= "</ul></div>\n";
}
//$output .= "</ul>\n";
}
}
Which prints the following structure:
<ul class="menu">
<li class="menu-item">
<a href="#">Menu Item</a>
<!-- wrapper-class around the first sub-menu -->
<div class="sub-menu__wrapper">
<ul class="sub-menu">
<li class="menu-item">Parent Menu Item</li>
<!-- the following items are supposed to be nested in the "parent menu item" before -->
<li class="menu-item">Child Menu Item</li>
<li class="menu-item">Child Menu Item</li>
<li class="menu-item">Child Menu Item</li>
<li class="menu-item">Parent Menu Item</li>
<!-- again the following items are supposed to be nested in the "parent menu item" before -->
<li class="menu-item">Child Menu Item</li>
<li class="menu-item">Child Menu Item</li>
<li class="menu-item">Child Menu Item</li>
...
</ul>
</div>
</li>
</ul>
As you can see in the comments I tried to alter the output, if the if-statement is false, but that messes up the output even more 😂
Any help, constructive criticism or nudge in the right direction is much appreciated 🙇