我的Wordpress, Bootstrap主题不与JavaScript一起工作


My Wordpress, Bootstrap theme is not working with JavaScript

我正在使用Bootstrap在本地主机上工作Wordpress主题。我已经正确地加载了CSS文件,我已经使用正确的方式加载了js文件,但由于某些原因js脚本不工作。

这是我的index.php

<?php get_header(); ?>

<?php get_footer(); ?>

这是我的header。php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href = "<?php bloginfo(stylesheet_url); ?>" rel = "stylesheet">
</head>

<body>
    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <!-- 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="#">Start Bootstrap</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="#about">About</a>
                    </li>
                    <li>
                        <a href="#services">Services</a>
                    </li>
                    <li>
                        <a href="#contact">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>

这里是我的footer。php

<script src= "<?php get_template_directory(); ?>/js/bootstrap.js"></script>
</body>
</html>
这个问题快把我逼疯了!我花了2个小时寻找解决方案,但一无所获!

那么问题是什么呢?

您可以使用它来包含您的脚本

function WM_Scripts(){
    wp_register_script( 'jScript', 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js' );
    wp_register_script('bootstrapjs',get_template_directory_uri() .'/js/bootstrap.min.js');
    wp_enqueue_script( 'jScript' );
    wp_enqueue_script( 'bootstrapjs' );
}
add_action( 'wp_enqueue_scripts', 'WM_Scripts' );

对于样式表,你必须使用这个函数。

function WM_CSS(){
    wp_register_style( 'bootstrapGrid', get_template_directory_uri() . '/css/bootstrap.min.css' );
    wp_enqueue_style( 'bootstrapGrid' );
}
add_action( 'wp_enqueue_scripts', 'WM_CSS' );

请参考wordpress文档将您的脚本包含到页脚。这是包含脚本和样式的正确方法。请添加wp_head();

将此代码放入"functions.php"文件中,以排队"/css/bootstrap.min.css""/style.css""/js/bootstrap.js":

/**
 * Enqueue scripts and styles
 */
function theme_scripts() {
    wp_enqueue_style( 'bootstrapcss', get_template_directory_uri() . '/css/bootstrap.min.css' );
    wp_enqueue_style( 'stylesheet', get_stylesheet_uri() );
    wp_enqueue_script(
            'bootstrapjs',
            get_stylesheet_directory_uri() . '/js/bootstrap.js',
            array(),
            '3.3.5', // Bootstrap version number
            true // Place before </body>
    );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

请注意,您需要将wp_enqueue_script的最后一个参数设置为true,以便将'/js/bootstrap.js'放在</body>结束标记之前。

您错过了echo。必须打印函数get_template_directory_uri()返回的内容,如下所示:

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/bootstrap.js"></script>