Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bootstrap navbar to WordPress menu

I know there are a lot of topics about this on the net but I find them very complicated. Basically I want to convert a Bootstrap navigation menu to a WordPress Menu.

Say I have the default Bootstrap Navbar:

<nav class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="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>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="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>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

The way I would normally link this navbar with my WordPress pages is instead of manually listing each <li> I would use the following:

    <ul class="nav navbar-nav">
     <?php wp_list_pages('title_li=');?>
     </ul>

The output of this would list all my pages I created in WordPress:

 <ul class="nav navbar-nav">
    <li class="page_item page-item-9"><a href="...">About</a></li>
    <li class="page_item page-item-2"><a href="...">Sample Page</a></li>
  </ul>

This is all fine as I can add a page and it gets included in my menu as expected.

The problem

The problem is I don't know how to include a dropdown item in the menu bar and integrate that into WordPress, for example the dropdown item:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
     <ul class="dropdown-menu" role="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>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
     </ul>
  </li>

How would I integrate this with WordPress so next time if I want to add a new dropdown list I can easily do it the same way as the normal menu items?

I would appreciate it if you don't provide links to WordPress codex websites and other tutorials as I have already tried many things

like image 761
user3574492 Avatar asked Aug 15 '14 18:08

user3574492


People also ask

How do I add bootstrap navbar to my WordPress theme?

Now, after that, you can copy the file bootstrap-navwalker from that repo inside your theme's directory, ie, inside /wp-content/themes/theme-name/ folder. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-content" aria-controls="navbar-content" aria-expanded="false" aria-label="<?


2 Answers

You need to use this https://github.com/twittem/wp-bootstrap-navwalker , add the nav walker file and follow the instructions. Here's a sample from a random site I made, I'm not adjusting it to your own site because you'll need to learn this for all your future WP developments. It's incredible easy, check it out:

    <div id="nav">
    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
      <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="<?php bloginfo('url'); ?>"><img class="logo" src="<?php bloginfo('template_directory'); ?>/images/logo.png" alt="" /></a>
        </div>


        <?php
        wp_nav_menu( array(
                'menu'              => 'primary',
                'depth'             => 2,
                'container'         => 'div',
                'container_class'   => 'navbar-collapse collapse',
                'menu_class'        => 'nav navbar-nav',
                'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                'walker'            => new wp_bootstrap_navwalker())
            );
        ?>
      </div><!-- /.container-fluid -->
    </nav>
</div><!-- #nav -->

As you may have noticed, basically you have to replace what is after your code's

 <!-- Collect the nav links, forms, and other content for toggling -->

with

<?php
    wp_nav_menu( array(
            'menu'              => 'primary',
            'depth'             => 2,
            'container'         => 'div',
            'container_class'   => 'navbar-collapse collapse',
            'menu_class'        => 'nav navbar-nav',
            'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
            'walker'            => new wp_bootstrap_navwalker())
        );
    ?>

and voila

like image 145
Devin Avatar answered Nov 15 '22 18:11

Devin


To fix the dropdown menu problem you should:

  1. go to class-wp-bootstrap-navwalker.php file

  2. look for $atts['data-toggle'] ="dropdown"; and replace it with $atts['data-bs-toggle'] ="dropdown";

like image 41
moaz studio استديو معاذ Avatar answered Nov 15 '22 17:11

moaz studio استديو معاذ