I am trying to build a WordPress theme with boostrap, but the nav menu is giving me some trouble. I would like the dropdown to work like boostraps dropdown, but I do not know how to do this.
This is my menu:
<ul class="nav">
<?php wp_nav_menu( array( 'items_wrap' => '%3$s' ) ); ?>
</ul>
This is what I have in my functions.php for the menu:
add_action('after_setup_theme', 'blankslate_setup');
function blankslate_setup(){
register_nav_menus(
array( 'main-menu' => __( 'Main Menu', 'blankslate' ) )
);
}
function my_wp_nav_menu_args( $args = '' )
{
$args['container'] = false;
return $args;
} // function
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
When I add a submenu item (via Appearance – Menus) this is what is currently generated:
<ul class="nav">
<li id="menu-item-18" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18"><a href="#">Parent</a>
<ul class="sub-menu">
<li id="menu-item-19" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-19"><a href="#">Child</a></li>
</ul>
</li>
</ul>
For my menu to work with bootstrap I need the menu to be generated like:
<ul class="nav">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Parent <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Child</a></li>
</ul>
</li>
</ul>
So I need:
- To add the class “dropdown”, if a menu-item has child-items.
- To add class=”dropdown-toggle” data-toggle=”dropdown” to the .dropdown
a element - To add
<b class="caret"></b>
after the Dropdown-menu name (inside the .dropdown a element) - To add
class="dropdown-menu"
to<ul class="sub-menu">
Hope someone can help!
You will need to write a custom walker extending
Walker_Nav_Menu
, more or less like so:Not sure of what you need with points 3 and 4, though.
To fix the dropdown menu problem you should:
go to
class-wp-bootstrap-navwalker.php
filelook for
and replace it with