Is there a way I can wrap a div around wp_nav_menu’s ul.sub-menu
So for example the markup would go from
<ul class="menu">
<li><a href="/">Item 1</a></li>
<li>
<a href="/">Item 2</a>
<ul class="sub-menu">
<li><a href="/">Item 1</a></li>
<li><a href="/">Item 1</a></li>
</ul>
</li>
<li><a href="/">Item 1</a></li>
<li><a href="/">Item 1</a></li>
</ul>
to this
<ul class="menu">
<li><a href="/">Item 1</a></li>
<li>
<a href="/">Item 2</a>
<div class="sub-menu-wrap">
<ul class="sub-menu">
<li><a href="/">Item 1</a></li>
<li><a href="/">Item 1</a></li>
</ul>
</div>
</li>
<li><a href="/">Item 1</a></li>
<li><a href="/">Item 1</a></li>
</ul>
Where “sub-menu-wrap” is the custom div.
Is this possible?
Use a custom walker, extend the methods
start_lvl()
andend_lvl
.Sample code, not tested:
Usage in your theme:
Proposed solution does not play nice with WordPress – WordPress default code and filters are ignored. I am giving here much more elegant solution, which keeps all filters like ‘nav_menu_submenu_css_class’ and other WP default code functioning: