如何将我的 javascript 放在页脚中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4221870/
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
How to put my javascript in the footer
提问by Trez
I just want to ask on how to print script 'javascript' at the footer using simple plugin. I'm using WordPress 3.0 any ideas?
我只想询问如何使用简单的插件在页脚处打印脚本“javascript”。我正在使用 WordPress 3.0 有什么想法吗?
回答by Jk_
Use a functions.php file inside your theme template add this :
在主题模板中使用 functions.php 文件添加以下内容:
<?php
function add_this_script_footer(){ ?>
[YOUR JS CODE HERE]
<?php }
add_action('wp_footer', 'add_this_script_footer'); ?>
Hope it helps!
希望能帮助到你!
回答by windyjonas
For an external javascript file to be linked in the footer, use this (>= WP2.8)
对于要在页脚中链接的外部 javascript 文件,请使用此 (>= WP2.8)
function my_javascripts() {
wp_enqueue_script( 'the-script-handle',
'path/to/file.js',
array( 'jquery','other_script_that_we_depend_on' ),
'scriptversion eg. 1.0',
true);
}
add_action( 'wp_enqueue_scripts', 'my_javascripts' );
That last true means that the script should be put at the wp_footer() hook.
最后一个 true 意味着脚本应该放在 wp_footer() 钩子上。
回答by Slapandthink
Hum may be it's too late to answer, but if anyone else comes here with the same problem :
嗯,现在回答可能为时已晚,但是如果其他人遇到同样的问题:
There is a plugin to do this : http://wordpress.org/extend/plugins/footer-javascript/
有一个插件可以做到这一点:http: //wordpress.org/extend/plugins/footer-javascript/
Or you can doing this manually by adding this short code in your functions.php :
或者您可以通过在您的 functions.php 中添加以下短代码来手动执行此操作:
/**
* Automatically move JavaScript code to page footer, speeding up page loading time.
*/
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);