请帮助我理解为什么会这样。
我已经在mu中加载了jQueryfunctions.php
-文件如下:
<?php
function load_jquery() {
wp_enqueue_script( \'jquery\' );
}
add_action( \'wp_enqueue_scripts\', \'load_jquery\' );
?>
我知道这是唯一正确的方法<但是现在问题来了
github page我下载了一个。要添加到我的网站的zip文件
为此,我必须在我的header.php
-文件:
<script src="<?php bloginfo(\'template_directory\')?>/js/modernizr-2.6.2.min.js"></script>
<script src="<?php bloginfo(\'template_directory\')?>/js/jquery-1.9.1.min.js"></script>
<script src="<?php bloginfo(\'template_directory\')?>/js/jquery.cookie-1.3.1.js"></script>
<script src="<?php bloginfo(\'template_directory\')?>/js/jquery.steps.js"></script>
我第一次这样做时,我忽略了
jquery-1.9.1.min.js
文件,因为我认为jQuery已经加载完毕。它不起作用。
在我添加了jQuery文件之后,整个事情变得像它应该的那样。
我不明白的是为什么我必须加载jQuery两次。
我使用WP 4.4.2
谁能给我解释一下吗?
----------更新-------------
所以我发现加载的顺序很重要<演示文件(github链接)按以下顺序说明:
<script src="../lib/modernizr-2.6.2.min.js"></script>
<script src="../lib/jquery-1.9.1.min.js"></script>
<script src="../lib/jquery.cookie-1.3.1.js"></script>
<script src="../build/jquery.steps.js"></script>
如果我更改
jquery-1.9.1.min.js
把它放在底部,一切都停止了。如果我把它放在上面
jquery.steps.js
一切又恢复了。
这是演示版。如果我将其转换为WordPress安装,则无法编辑wp_head()
.这是我的wp_head()
-输出如下所示:
<script src="http://192.168.104.105:8888/basic/wp-includes/js/jquery/jquery.js?ver=1.11.3" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1" type="text/javascript">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-parallax/1.1.3/jquery-parallax-min.js?ver=4.4.2" type="text/javascript">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-localScroll/1.3.5/jquery.localScroll.min.js?ver=4.4.2" type="text/javascript">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/modernizr-2.6.2.min.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/jquery.cookie-1.3.1.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/jquery.steps.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/modernizr.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/mootools.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/slimbox.js?ver=4.4.2" type="text/javascript">
<script src="http://192.168.104.105:8888/basic/wp-content/themes/basic_theme/js/modernizr_team.js?ver=4.4.2" type="text/javascript">
是否应更改的输出顺序(如果可能)
wp_head()
我还尝试加载jquery.steps.js
中的文件wp_footer()
但运气不好
登记和排队:
function pr_scripts_styles() {
wp_enqueue_script(\'jquery\'); // just enqueue as its already registered
wp_enqueue_script( \'jquery_parallax\', \'//cdnjs.cloudflare.com/ajax/libs/jquery-parallax/1.1.3/jquery-parallax-min.js\' );
wp_enqueue_script( \'jquery_localscroll\', \'//cdnjs.cloudflare.com/ajax/libs/jquery-localScroll/1.3.5/jquery.localScroll.min.js\' );
wp_enqueue_script( \'jquery_scrollto\', \'//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js\' );
wp_enqueue_script( \'jquery_scrollto\', \'//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.11/jquery.scrollTo.min.js\' );
/* REGISTER ALL JS FOR SITE */
wp_register_script(\'modernizr_2_6_2\',get_stylesheet_directory_uri().\'/js/modernizr-2.6.2.min.js\', array( \'jquery\' ));
wp_register_script(\'jquery_cookie_1_3_1\',get_stylesheet_directory_uri().\'/js/jquery.cookie-1.3.1.js\', array( \'modernizr_2_6_2\'));
wp_register_script(\'jquery_steps\',get_stylesheet_directory_uri().\'/build/jquery.steps.js\',array( \'jquery_cookie_1_3_1\' ),\'\',false);
wp_register_script(\'modernizr\',get_stylesheet_directory_uri().\'/js/modernizr.js\');
wp_register_script(\'mootools\',get_stylesheet_directory_uri().\'/js/mootools.js\');
wp_register_script(\'slimbox\',get_stylesheet_directory_uri().\'/js/slimbox.js\');
wp_register_script(\'modernizr_team\',get_stylesheet_directory_uri().\'/js/modernizr_team.js\');
/* REGISTER ALL CSS FOR SITE */
wp_register_style(\'font_awesome\',get_stylesheet_directory_uri().\'/font-awesome/4.5.0/css/font-awesome.css\');
wp_register_style(\'normalize\',get_stylesheet_directory_uri().\'/css/normalize.css\');
wp_register_style(\'main\',get_stylesheet_directory_uri().\'/css/main.css\');
wp_register_style(\'jquery_steps\',get_stylesheet_directory_uri().\'/css/jquery.steps.css\');
wp_register_style(\'slimbox_css\',get_stylesheet_directory_uri().\'/css/slimbox.css\');
wp_register_style(\'teammembers\',get_stylesheet_directory_uri().\'/css/teammembers.css\');
/* CALL ALL CSS AND SCRIPTS FOR SITE */
wp_enqueue_script(\'modernizr_2_6_2\');
wp_enqueue_script(\'jquery_cookie_1_3_1\');
wp_enqueue_script(\'jquery_steps\');
wp_enqueue_script(\'modernizr\');
wp_enqueue_script(\'mootools\');
wp_enqueue_script(\'slimbox\');
wp_enqueue_script(\'modernizr_team\');
wp_enqueue_style(\'font_awesome\');
wp_enqueue_style(\'normalize\');
wp_enqueue_style(\'main\');
wp_enqueue_style(\'jquery_steps\');
wp_enqueue_style(\'slimbox_css\');
wp_enqueue_style(\'teammembers\');
}
add_action( \'wp_enqueue_scripts\', \'pr_scripts_styles\' );