I'm using twitter bootstrap in my theme. I'm trying to use bootstrap menu in my theme. But it use custom attributes like data-toggle.

Here is the full code.

<ul class="nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li class="divider"></li>
                <li><a href="#">Separated link</a></li>

Can anyone help me to optimize this code for wp_nav_menu() ?


This is what i tried so far.

1) Custom submenu class

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"dropdown-menu\">\n";

2) wp_nav_menu

<?php wp_nav_menu( array( 'items_wrap' => '<ul class="nav">%3$s</ul>','walker' => new My_Walker_Nav_Menu() ) ); ?>

Can you help me to add that "dropdown" class and this link

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>


4 Answers 4


For a copy paste solution check this custom Walker out: http://goodandorgreat.wordpress.com/2012/01/12/update-2-using-twitter-bootstrap-dropdown-menus-with-wordpress/

It's missing one or two things like data-toggle="dropdown" and <b class="caret"></b>.

It should be quite easy to figure that out, but here's my modified version: https://gist.github.com/1817371

Hope that helps.

  • Thanks for this. I don't have the code in front of me, but I literally was pulling my hair out trying to figure out how to get a had_children property. Is this documented anywhere by chance? (the post WP3.4 version I mean)
    – Jon Jaques
    Commented Feb 24, 2013 at 15:22
  • I'm not sure, but @Jan Fabry has a short explanation in this thread
    – Duane
    Commented Apr 11, 2013 at 15:30

I used a different approach than a custom walker. I used a function to add the class "dropdown" to any parent nav items which contain sub menu items. Since WordPress outputs the class "sub-menu" by default, I use jQuery to add the class "dropdown-menu" to any thing that has a class of "sub-menu". I then use jQuery to append the <b class="caret"></b> element.

I have written this up as a complete tutorial about how to implement the Bootstrap dropdown in a WordPress nav menu here: http://astronautweb.co/2012/10/wordpress-dropdown-bootstrap/


See this answer for details: Look for menu_item_parent and set class="dropdown-toggle" conditionally.

More similar questions.

  • Hello toscho, thanks. Check this page. That is what i exactly looking for. I tried those codes. Since i'm not a php expert i couldn't make it work. Can you help me with this answer? Thanks Commented Feb 3, 2012 at 21:41

Try WP Bootstrap Menu if your not looking for something complicated.

You have to replace the native wp_nav_menu() with a custom function from the plugin in your theme to get it working. Be aware the plugin function is not compatible with other plugins, it doesn’t offer the same filters as the WordPress walker.

  • An answer should be more than just a link to an external site. Please add a solution.
    – fuxia
    Commented Jan 23, 2013 at 4:59
  • Ah sorry I didn't know, I don't know what to add, that plugin generates exact code with a custom function that's needed to work with bootstrap
    – farhan
    Commented Jan 24, 2013 at 6:15
  • I made an edit to show how it could look: describe how it works and what the most important changes are. Still not ideal, but later readers can judge faster if they want to use it.
    – fuxia
    Commented Jan 24, 2013 at 6:23
  • Thank you, I'll add details like that next time when I post
    – farhan
    Commented Jan 24, 2013 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.