【问题标题】:Bootstrap Navbar not working in Wordpress ThemeBootstrap 导航栏在 Wordpress 主题中不起作用
【发布时间】:2018-09-16 16:53:24
【问题描述】:

我正在尝试学习使用 Bootstrap 为 Wordpress 主题创建导航栏。我只想制作我的菜单而不使用 Navwalker。我使用了 W3 学校的一个简单的 Bootstrap 示例,但它不起作用。这是所需的导航栏。

想要的结果:

我的标题中有相同​​的代码。

<!doctype html>
<html <?php language_attributes();?>>
   <head>
      <meta charset="<?php bloginfo('charset'); ?>">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="description" content="<?php bloginfo('description'); ?>">
      <title><?php bloginfo('name'); ?> |
         <?php is_front_page() ? bloginfo('description') : wp_title(); ?>
      </title>
      <!-- Bootstrap core CSS -->
      <link href="<?php bloginfo('template_url');?>/css/bootstrap.css" rel="stylesheet">
      <!-- Custom styles for this template -->
      <link href="https://fonts.googleapis.com/css?family=Playfair+Display:700,900" rel="stylesheet">
      <link href="<?php bloginfo('stylesheet_url')?>" rel="stylesheet">
      <?php wp_head(); ?>  <!-- This adds other information like plugins to the head -->
   </head>
   <body>
      <div class="container">
      <div class="row">
      </div>
      <nav class="navbar navbar-default">
         <div class="container-fluid">
            <div class="navbar-header">
               <a class="navbar-brand" href="#">WebSiteName</a>
            </div>
            <ul class="nav navbar-nav">
               <li class="active"><a href="#">Home</a></li>
               <li><a href="#">Page 1</a></li>
               <li><a href="#">Page 2</a></li>
               <li><a href="#">Page 3</a></li>
            </ul>
         </div>
      </nav>
      <div class="row">
         hello world
      </div>
      </header>

这就是我的导航栏的样子

我的结果:

【问题讨论】:

    标签: wordpress twitter-bootstrap-3 themes navbar


    【解决方案1】:

    尝试通过主题的函数将文件排入队列,而不是将它们添加到 header.php。

    // Load main files  
    function enqueue_styles_and_scripts() {
    
       // Bootstrap
       wp_enqueue_style ('bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
       wp_enqueue_script    ('bootstrap_js',  get_template_directory_uri() . '/assets/js/bootstrap.min.js', array('jquery'), 1.1, true );
    
       // Main files
       wp_enqueue_style ('style_css',   get_template_directory_uri() . '/style.css' );
       wp_enqueue_script    ('main_js', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), 1.1, true );
    
    }
    
    add_action( 'wp_enqueue_scripts', 'enqueue_styles_and_scripts' );
    

    【讨论】:

      【解决方案2】:

      假设您的引导 css 位于 css 文件夹下的主题目录中,请尝试将链接更改为:

      <link href="<?php echo get_template_directory_uri(); ?>/css/bootstrap.css" rel="stylesheet">
      

      【讨论】:

      • 这不是在 WP 主题中加载脚本的方式。
      • 是的,但是如果您看上面有人已经建议将样式表排入队列,这是首选和建议的方法,我正在提供对使用的初始方法的修复...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2018-06-18
      • 2013-07-28
      • 2017-08-31
      • 1970-01-01
      相关资源
      最近更新 更多