I’m working on a fallback menu for a WordPress theme I’m working on. The main menu’s menu_class
renders a ul
which is what I want.
However, if I use the menu fallback, the menu_class
renders a div
instead. Is there a way to make WordPress render a ul
for the fallback menu instead of a div
?
Here’s my code:
function clarity_main_nav_fallback() {
wp_nav_menu(array(
'show_home' => true,
'container' => false, // remove nav container
'container_class' => 'menu clearfix', // class of container (should you choose to use it)
'menu_class' => 'nav navbar-nav' // adding custom nav class
));
}
First, I think you’re confusing the
wp_nav_menu()
args. The'menu_class'
parameter defines the class added to the menu element, which by default is<ul>
, as per the'items_wrap'
parameter. The default'menu_class'
is'menu'
, which results in<ul class="menu">
.The real issue is actually the
fallback_cb
– the callback used when no menu is defined – which defaults towp_page_menu()
.Now, both
wp_nav_menu()
andwp_page_menu()
output an unordered list (<ul>
) wrapped by a container (<div>
) by default. In your call towp_nav_menu()
, you have:'container' => false
, which overrides the menu list being wrapped by a container<div>
. But that parameter does not get passed to the defaultwp_page_menu()
callback output.The easiest solution would be to define your own callback:
Then declare your callback function:
Now, here’s the caveat:
wp_nav_menu()
applies the'menu_class'
parameter to the unordered list(
<div><ul class="$menu_class"></ul></div>
)wp_page_menu()
applies the'menu_class'
parameter to the wrapper container outside the menu list(
<div class="$menu_class"><ul></ul></div>
)So, you’ll need to account for that with your CSS.
You have a problem with menu location
Go to