WordPress Enqueue Js 脚本
我在使 wp_enqueue 函数正常工作时遇到问题。我已经查看了所有相关文档,但无法筛选并找出应该放在哪里的内容。
到目前为止,我明白我应该从我正在创建的主题的functions.php 文件中注册和排队文件。这正是我所做的。我创建了一些 PHP 标签并将其放在页面底部的中间。保存并上传。
当我重新加载时,它只是返回一个空白的白屏,一定是代码中的错误或其他错误。
这是函数:
<?php
function add_scripts(){
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_register_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_register_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_register_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_register_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_register_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_register_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_register_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
wp_enqueue_script('jquery');
wp_enqueue_script('nivo');
wp_enqueue_script('slimbox');
wp_enqueue_script('picasa');
wp_enqueue_script('pwi')
wp_enqueue_script('swf');
wp_enqueue_script('simpletube')
wp_enqueue_script('jqvalidate');
}
add_action('init','add_scripts');
?>
那么我的语法是否存在某种问题?我对 PHP 不是那么强。 非常感谢任何帮助。谢谢!
I am having trouble getting wp_enqueue functions to work. I've looked at all the documentation on it but am having trouble sifting through and finding out what is supposed to go where.
so far I understand that I am supposed to register and enqueue the files from the functions.php file of the theme I am creating. So that is exactly what I do. I create some PHP tags and drop it in the middle of them, at the bottom of the page. Save and Upload.
When I reload, it just returns a blank white screen, must be an error in the code or something.
Here is the function:
<?php
function add_scripts(){
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_register_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_register_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_register_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_register_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_register_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_register_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_register_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
wp_enqueue_script('jquery');
wp_enqueue_script('nivo');
wp_enqueue_script('slimbox');
wp_enqueue_script('picasa');
wp_enqueue_script('pwi')
wp_enqueue_script('swf');
wp_enqueue_script('simpletube')
wp_enqueue_script('jqvalidate');
}
add_action('init','add_scripts');
?>
So is there some sort of problem with my syntax? I'm not that strong with PHP.
Any help is greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在看不到整个文件的情况下调试它有点困难,但事实上你得到一个“空白页”表明某处肯定存在比语法问题更大的问题。
你确实有正确嵌套的 php 标签吗?即
会给你带来问题。
另外,现在的常见做法是从文件末尾保留最后一个
?>
(这意味着在结束标记后留有空格不会有任何问题,而且它们不是必需的)其中,您已经使用了
wp_register_script('jquery'...)
- WordPress 已经注册了 jquery。如果您想重新注册,您需要先wp_deregister_script('jquery')
。我也只会在管理员之外执行此操作,因此:if(!is_admin()){wp_deregister_script('jquery'); <你的 wp_register_script 内容> }
如果这些方法没有帮助,请复制并粘贴整个
functions.php
文件(使用pastebin.com 并给我们一个链接)顺便说一句,您正在使用
get_bloginfo('url')
多次 - 这意味着您对数据库运行了大量不必要的调用。将其放入变量中并节省一点开销:哦!另一件事,我不认为
url
是get_bloginfo()
允许的参数,我认为你想要wpurl
有关 get_bloginfo() 函数的 Codex 页面
祝你好运!
It's kind of hard to debug it without seeing the whole file but the fact you get a 'blank page' suggests there's definitely something larger than a syntax problem somewhere.
Do you definitely have correctly nested php tags? i.e.
will give you problems.
Also, it's now common practice to leave the last
?>
from the end of the file (it means you wont have any issues with having whitespace after the closing tags and they're not necessary)On top of that, you've used
wp_register_script('jquery'...)
- WordPress already has jquery registered. If you wish to re-register it, you need towp_deregister_script('jquery')
first. I'd also only do that outside of the admin, so:if(!is_admin()){wp_deregister_script('jquery'); <your wp_register_script stuff> }
If these things don't help, copy and paste your entire
functions.php
file (use pastebin.com and give us a link)As an aside, you're using
get_bloginfo('url')
several times - which means you're running lots of unnecessary calls to the database. Stick it into a variable and save yourself a little overhead:Oh! One more thing, I don't think
url
is an allowed argument forget_bloginfo()
I think you wantwpurl
Codex page on get_bloginfo() function
Good luck!
以下两行缺少
;
:Missing
;
for the following two lines:我将使用而不是您的代码:
所以请注意,我已经删除了“wp_register_script”,因为如果您要在注册后立即调用 wp_enqueue ,则完全没有必要使用它。
使用它是为了以后您可以在代码中的任何其他地方调用它,而不包含路径。
还有一个很大的变化是我不是从 调用该函数
但我是从 调用它
另外请考虑向您的 wp_enqueue_script 添加其他参数,例如
Instead of your code I would use:
So please notice I have removed "wp_register_script" as using that is totally unnecessary if you are going to call wp_enqueue immediately after register.
Is used so that you can afterwards call it ANYWHERE else in code without including the path.
Also big change is that I'm not calling the function from
But I'm calling it from
Also please consider adding additional parameters to your wp_enqueue_script such as