Wordpress Navbar - 折叠按钮没有出现Bootstrap

时间:2014-12-15 15:10:13

标签: jquery html css wordpress twitter-bootstrap

我试图在自定义主题中设置引导导航栏,但由于某种原因无法弄清楚为什么这不起作用。

这是header.php源代码中的代码。

(注意:导航栏效果很好,但在移动设备中没有折叠导航栏按钮)

提前致谢

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title><?php echo get_the_title( $ID ); ?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="<?php bloginfo('stylesheet_url');?>" rel="stylesheet">
  <?php wp_enqueue_script("jquery"); ?>
  <?php wp_head(); ?>
</head>
<body>

<div id="header">
  <div class="navbar navbar-fixed-top" role="navigation">
      <div class="container">
        <button class="btn btn-navbar" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand"><img src="<?php bloginfo('template_directory'); ?>/img/logo.png" 
         alt="Electricians in Cardiff" class="img-responsive"></a>
        <div class="collapse navbar-collapse navbar-collapse" id="myNavbar">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="/index.php">Home</a></li>
            <li><a href="/about/">About</a></li>
            <li><a href="/services/">Services</a></li>
            <li><a href="/blog/">Blog</a></li>
            <li><a href="/areas-we-cover/">Areas</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
  </div>
</div>

<div class="container">

我遵循了一个教程,这就是我放在样式表中的内容:

@import url('bootstrap/css/bootstrap.css'); 
@import url('bootstrap/css/bootstrap-responsive.css'); 
body { 
    padding-top: 70px; 
}

,这是在functions.php

<?php 

function wpbootstrap_scripts_with_jquery()
{
    // Register the script like this for a theme:
    wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/js/bootstrap.js', array( 'jquery' ) );
    // For either a plugin or a theme, you can then enqueue the script:
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );


if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
?>

1 个答案:

答案 0 :(得分:0)

代码中存在一些错误:

<button class="btn btn-navbar" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">>

应该是:

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">

注意2&#34;类&#34;属性:class =&#34; btn btn-navbar&#34;和class =&#34; navbar-toggle&#34;

此外,还有一些DIV错位。

清理代码:

<div id="header">
    <div class="navbar navbar-fixed-top" role="navigation">
        <div class="container">

           <div class="navbar-header">
               <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                 <span class="icon-bar"></span>
                 <span class="icon-bar"></span>
                 <span class="icon-bar"></span>
               </button>
               <a class="navbar-brand"><img src="/img/logo.png" 
alt="Electricians in Cardiff" class="<?php bloginfo('template_directory'); ?>/img-responsive"></a>
           </div>

           <div class="collapse navbar-collapse" id="myNavbar">
              <ul class="nav navbar-nav navbar-right">
                 <li><a href="/index.php">Home</a></li>
                 <li><a href="/about/">About</a></li>
                 <li><a href="/services/">Services</a></li>
                 <li><a href="/blog/">Blog</a></li>
                 <li><a href="/areas-we-cover/">Areas</a></li>
               </ul>
           </div><!--/.nav-collapse -->
        </div>
    </div>
</div>