I am very new to wordpress themeing . I am trying to create a Twitter Bootstrap
menu to my newly created theme as below in header.php
page.
$defaults = array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'navbar-collapse collapse',
'menu_id' => 'navbar',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $defaults );
With the above codes, I am expecting to add a class to navbar-collapse collapse
to ul
, but instead it produces HTML
as below :
<div class="navbar-collapse collapse"><ul><li class="page_item page-item-11">
etc. How can I add class ul
?
Check you’ve actually registered ‘header-menu’ via register_nav_menus.
Removing theme_location => ‘header-menu’ will resolve the issue until you’ve registered your navigation ID correctly.
You should have this in your functions.php
Try checking out a function that’s already been made like this one. It will create the correct syntax for the menu to work.
Due to the fact that no one solved your problem. I want to provide a solution for future users. The solution is in the container parameter of the wp_nav_menu function.
Documentation:
https://developer.wordpress.org/reference/functions/wp_nav_menu/
To make parameter menu_class add it`s valuse to correct element, in your case it is ul element, you have to define container parameter as div, like below. Then parameter container_class will have default class ‘menu-{menu slug}-container’ because it`s not have any value given. container_id parameter also will be empty becouse there is no value given.
Then menu_class and menu_id parameters will be added to the ul element and ul element will look this
<ul id="navbar" class="navbar-collapse collapse">
.In case you don`t want to have div container delete container_class and container_id and leave container parameter with ul value like this: