wordpress 页脚中的 wp_enqueue_script

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9775104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:53:58  来源:igfitidea点击:

wp_enqueue_script in the footer

wordpresshookactionfooter

提问by jasonaburton

Having problems enqueuing a script in the footer.

在页脚中排队脚本时遇到问题。

wp_deregister_script('jquery');
wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), false, true);

Here's the wp_enqueue_script definition:

这是 wp_enqueue_script 定义:

wp_enqueue_script( 
     $handle
    ,$src
    ,$deps
    ,$ver
    ,$in_footer 
);

As you can see I am setting $in_footer to true. This does not work for me. Without that argument it works fine and puts it in header.php. Why doesn't it work with $in_footer?

如您所见,我将 $in_footer 设置为 true。这对我不起作用。没有那个参数它工作正常并将它放在 header.php 中。为什么它不适用于 $in_footer?

回答by Com1BOUTIK

By default WordPress default JavaScripts won't move to the footer, a workaround is to add its path :

默认情况下 WordPress 默认 JavaScript 不会移动到页脚,解决方法是添加其路径:

wp_enqueue_script('jquery','/wp-includes/js/jquery/jquery.js','','',true);

See detailled post about this

请参阅有关此的详细帖子

回答by Nick

Make sure you have the wp_footer()right before the </body>tag. See the $in_footer parameterfor more info. You also need to call this before wp_head has run. Try also using this action.

确保你wp_footer()</body>标签之前有权利。有关详细信息,请参阅$in_footer 参数。您还需要在 wp_head 运行之前调用它。也尝试使用此操作。

add_action('wp_enqueue_scripts', 'add_scripts_to_pages');

Another thing to try is using NULLas 3rd and 4th parameters.

另一件要尝试的事情是使用NULL第三和第四个参数。

回答by ewroman

As an addition to @Nick's answer;

作为@Nick 回答的补充;

if you have a PHP problem before wp_footer()you wont be able to see your script in footer. Source is https://wordpress.org/support/topic/enqueuing-scripts-in-footer-does-not-working-with-wordpress-36.

如果您在wp_footer()无法在页脚中看到脚本之前遇到PHP 问题。来源是https://wordpress.org/support/topic/enqueuing-scripts-in-footer-does-not-working-with-wordpress-36