为wordpress主题创建一个bootstrap导航栏

时间:2017-12-06 04:04:31

标签: php css wordpress twitter-bootstrap

我正在努力创造自己的新闻主题。目前我无法让我的导航栏与bootstrap一起正常工作。我无法从我的style.css文件中设置样式,并且当网站进入移动版本时,该按钮不会展开导航栏以供用户查看。

这是我的header.php中的导航栏:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <?php /* Primary navigation */
    wp_nav_menu( array(
      'menu' => 'top_menu',
      'depth' => 2,
      'container' => false,
      'menu_class' => 'nav',
      //Process nav menu using our custom nav walker
      'walker' => new wp_bootstrap_navwalker())
    );
    ?>

      </div>
    </nav>

这是我在functions.php中的功能:

// Register Nav Walker class_alias
require_once('wp_bootstrap_navwalker.php');

// Theme Support
function wpb_theme_setup(){
  add_theme_support('post-thumbnails');

  // Nav Menus
  register_nav_menus(array(
    'primary' => __('Primary Menu')
  ));

  // Post Formats
  add_theme_support('post-formats', array('aside', 'gallery'));
}

add_action('after_setup_theme','wpb_theme_setup');

我听说过下拉功能,我需要使用bootstrap nav walker,这就是它存在的原因。

1 个答案:

答案 0 :(得分:0)

function.php

register_nav_menus( array(
  'primary' => esc_html__( 'Primary', 'yourwebsite' ),
) );

require_once('wp_bootstrap_navwalker.php');

的header.php

<header id="masthead" class="site-header" role="banner">
    <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="<?php echo home_url(); ?>">
                    <?php bloginfo('name'); ?>
                </a>
        </div>

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