Two-part Bootstrap navigation menu with logo in th

2019-05-14 19:10发布

问题:

I am making a dynamic wordpress links in navigation bar using nav_walker. How can i implement center something like this in wordpress nav_walker:

+-----------------------------+-----------+------------------------------+
+---------------HOME--ABOUT---|-LOGO HERE-|---CONTACT--PROFILE-----------+
+-----------------------------+-----------+------------------------------+

How can i do that or something provide me jsfiddle to come up with that please. Is it two nav_walker function for left and right navigation? how can i insert the navbar brand in center? can anyone give me structure. thanks.

my code:

<nav class="navbar navbar-inverse navbar-fixed-top">
    <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>
    </div>

    <div class="navbar-brand navbar-center">

    </div>

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

回答1:

It can by suprisingly simple if you have same amount of items on left and on right. Just create a list, put the logo in the middle and do text-align: center. See here: https://jsfiddle.net/DTcHh/26132/

The trouble starts when the left and right are even slightly uneven. I suggest creating two lists, each for half of the screen. They will be aligned towards the center and leave a space in the center for the logo.

Check it out here: https://jsfiddle.net/DTcHh/26133/

html:

<div class="navbar navbar-default">
    <div class="logo-wrapper">
        <div class="logo"><img class="logo" src="http://www.hsdtaxlaw.com/wp-content/uploads/2016/05/logo_placeholder.png" alt=""></div>
    </div>
    <div class="half">      
        <ul class="left-navlist">
            <li>Home</li>
            <li>About</li>
        </ul>
    </div>
    <div class="half">      
        <ul class="right-navlist">
            <li>Contact</li>
            <li>Profile</li>
            <li>Maps</li>
        </ul>
    </div>
</div>

css:

.logo-wrapper {
    text-align: center;
    margin-bottom: -37px;
}

.logo {
    width: 100px;
    display: inline-block;
}

.navbar li {
    display: inline-block;
    padding: 0 10px 10px;
}

.half {
    width: 50%;
    display: block;
    float: left;
}

.right-navlist {
    padding-left: 60px;
}

.left-navlist {
    text-align: right;
    padding-right: 60px;
}

As for how to implement this with wordpress, I believe you will have to get rid of the navwalker plugin. Just create two menus in wordpress: left-primary and right-primary. Then just iterate them with wp_get_nav_menu_items('left-primary') and build the menu as I suggested. Here is the php code without the nav-walker:

<div class="navbar navbar-default">
    <div class="logo-wrapper">
        <div class="logo"><img class="logo" src="http://www.hsdtaxlaw.com/wp-content/uploads/2016/05/logo_placeholder.png" alt=""></div>
    </div>
    <div class="half">      
        <ul class="left-navlist">
            <?
                $menu = wp_get_nav_menu_items('left-primary');
                foreach ($menu as $menu_item) {
                    $title = $menu_item->title;
                    $link = $menu_item->url;
                    ?>
                        <li href="<?=$link?>"><?=$title?></li>
                    <?
                ?
            ?>
        </ul>
    </div>
    <div class="half">      
        <ul class="right-navlist">
            <?
                $menu = wp_get_nav_menu_items('right-primary');
                foreach ($menu as $menu_item) {
                    $title = $menu_item->title;
                    $link = $menu_item->url;
                    ?>
                        <li href="<?=$link?>"><?=$title?></li>
                    <?
                ?
            ?>
        </ul>
    </div>
</div>


回答2:

A while ago I was looking for the exact same thing and came across this pen. props to Bryan Willis

Bootstrap 3 Headers Look at last example !

HTML

<div class="container">
<nav class="navbar navbar-default">
<div class="container-fluid">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar9">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
  </div>

  <div class="brand-centered">
  <a class="navbar-brand" href="http://disputebills.com"><img style="margin-right: 10px; padding: 0;" src="http://res.cloudinary.com/disputebills/image/upload/v1462474206/blue-mark_cnzgry.png" alt="Dispute Bills">Dispute Bills
  </a>
  </div>

  <div id="navbar9" class="navbar-collapse collapse">
    <ul class="nav navbar-nav navbar-left">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
    </ul>
  </div>
  <!--/.nav-collapse -->
</div>
<!--/.container-fluid -->

CSS

.navbar-brand {
  padding: 0px;
}
.navbar-brand>img {
  height: 100%;
  padding: 15px;
  width: auto;
}
/* EXAMPLE 9 - Center with Flexbox and Text*/
.brand-centered {
  display: flex;
  justify-content: center;
  position: absolute;
  width: 100%;
  left: 0;
  top: 0;
}
.brand-centered .navbar-brand {
  display: flex;
  align-items: center;
}


回答3:

Ok try using the below markup to achieve what you are looking for, you also need to tweak a little of CSS to override the bootstrap's default styles.

HTML

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
         <li class="active"><a href="#">Home</a></li>
         <li><a href="#about">About</a></li>
         <a class="navbar-brand">Your logo</a>
         <li><a href="../navbar/">Contact</a></li>
         <li><a href="../navbar/">Profile</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

The CSS which achieves what you want(make sure this overrides the default bootstrap styles)

.navbar-nav {
    float: none;
    margin: auto;
    max-width: 40%; //adjust to your requirements
}

Codepen here