两部分Bootstrap导航菜单,中间有徽标

时间:2016-10-11 08:57:38

标签: html css wordpress twitter-bootstrap wp-nav-walker

我正在使用nav_walker在导航栏中制作动态wordpress链接。我如何在wordpress nav_walker中实现这样的中心:

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

我怎么能做到这一点或者某些东西给我提供帮助呢? 左右导航是否有两个nav_walker功能?我如何在中心插入导航栏品牌?任何人都可以给我结构。感谢。

我的代码:

<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>

3 个答案:

答案 0 :(得分:5)

如果您在左侧和右侧拥有相同数量的物品,则可以非常简单。只需创建一个列表,将徽标放在中间并执行text-align: center。见这里:https://jsfiddle.net/DTcHh/26132/

当左右两侧甚至略微不平时,问题就开始了。我建议创建两个列表,每个列表用于屏幕的一半。它们将朝向中心对齐,并在徽标的中心留出空间。

请在此处查看: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;
}

至于如何用wordpress实现这个,我相信你将不得不摆脱navwalker插件。只需在wordpress中创建两个菜单:left-primaryright-primary。然后用wp_get_nav_menu_items('left-primary')迭代它们并按照我的建议构建菜单。这是没有nav-walker的php代码:

<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>

答案 1 :(得分:0)

好的尝试使用下面的标记来实现您的目标,您还需要调整一些CSS来覆盖bootstrap的默认样式。

<强> 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>

CSS 可以实现您的目标(确保这会覆盖默认的引导程序样式)

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

Codepen here

答案 2 :(得分:0)

前段时间我正在寻找完全相同的东西并遇到了这支笔。 道具到Bryan Willis

Bootstrap 3 Headers看看最后一个例子!

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;
}