The wp_nav_menu produces default classes for the <div>
and <ul>
producing the following ….
<div class="menu-horizontal-container">
<ul id="menu-horizontal" class="menu">
<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14"><a href="#">Menu 1</a></li>
<li id="menu-item-18" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18"><a href="#">Menu 2</a></li>
<li id="menu-item-19" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-19"><a href="#">Menu 3</a></li>
</ul>
</div>
… but I want to change the classes and IDs to the following …. how can this be done?
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right" id="main_navigation_menu">
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a></li>
<li><a href="#">Menu 3</a></li>
</ul>
</div>
Add it where you want to display menu.
Add walker to function.php
Here is the solution which combines the contributions from @Oleg and @Nathan with two possible outputs … also the important thing was declaring the theme location rather than the menu name.
OUTPUT A – without the Walker_Nav_Menu function
produced:
OUTPUT B – with the Walker_Nav_Menu function BUT changing ‘echo’ to TRUE in the parameters:
produced:
Thanks @Oleg and @Nathan.
You need to adjust the parameters you pass in to
wp_nav_menu
. In particular you need to set ‘menu_class’, ‘menu_id’ and ‘container_class’.Example:
Further reading: https://developer.wordpress.org/reference/functions/wp_nav_menu/