[英]My Wordpress, Bootstrap theme is not working with JavaScript
I'm working on Wordpress theme using Bootstrap on localhost. 我正在使用本地主机上的Bootstrap处理Wordpress主题。 I have load the CSS files correctly and I have load the js files using the right way but for some reasons js scripts is not working.
我已经正确加载了CSS文件,并且已经使用正确的方式加载了js文件,但是由于某些原因,js脚本无法正常工作。
Here is my index.php 这是我的index.php
<?php get_header(); ?>
<?php get_footer(); ?>
Here is my header.php 这是我的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>
Here is my footer.php 这是我的footer.php
<script src= "<?php get_template_directory(); ?>/js/bootstrap.js"></script>
</body>
</html>
This problem drives me CRAZY! 这个问题使我发疯! I spent 2 hours searching for solution but nothing!
我花了2个小时寻找解决方案,但一无所获!
So what is the problem guys? 那么,有什么问题的家伙?
You can use it for including your scripts 您可以使用它来包含脚本
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' );
And For stylesheet you have to use this function. 对于样式表,您必须使用此功能。
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' );
Please refer wordpress documentation for include your script into footer. 请参考wordpress文档,以将脚本包含在页脚中。 This the right way to include your script and styles.
这是包含脚本和样式的正确方法。 And please add wp_head();
并请添加wp_head(); functions in your header to load script and styles.
标头中的函数以加载脚本和样式。
Put this code in your "functions.php" file to enqueue "/css/bootstrap.min.css" , "/style.css" and your "/js/bootstrap.js" : 把这个代码在你的“的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' );
Keep note, you need to set the last parameter of wp_enqueue_script
to true
to place your '/js/bootstrap.js' before the </body>
end tag. 请注意,您需要将
wp_enqueue_script
的最后一个参数设置为true
以将'/js/bootstrap.js'放置在</body>
结束标记之前。 For more info visit this link . 有关更多信息,请访问此链接 。
You missed echo
. 你错过了
echo
。 You must print what the function get_template_directory_uri()
returns like here: 您必须像下面这样打印函数
get_template_directory_uri()
返回的内容:
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/bootstrap.js"></script>
After that your js will work flawless. 之后,您的js将可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.