WordPress Enqueue Js 脚本

发布于 2024-11-05 16:48:32 字数 1226 浏览 1 评论 0原文

我在使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光是把杀猪刀 2024-11-12 16:48:32

在看不到整个文件的情况下调试它有点困难,但事实上你得到一个“空白页”表明某处肯定存在比语法问题更大的问题。

你确实有正确嵌套的 php 标签吗?即

<?php

some code

<?php

some more code

?>

some more code

?>

会给你带来问题。

另外,现在的常见做法是从文件末尾保留最后一个 ?> (这意味着在结束标记后留有空格不会有任何问题,而且它们不是必需的

)其中,您已经使用了 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') 多次 - 这意味着您对数据库运行了大量不必要的调用。将其放入变量中并节省一点开销:

$my_url = get_bloginfo('wpurl');

wp_register_script('thing', $my_url.'/script/location/file.js');

哦!另一件事,我不认为 urlget_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.

<?php

some code

<?php

some more code

?>

some more code

?>

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 to wp_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:

$my_url = get_bloginfo('wpurl');

wp_register_script('thing', $my_url.'/script/location/file.js');

Oh! One more thing, I don't think url is an allowed argument for get_bloginfo() I think you want wpurl

Codex page on get_bloginfo() function

Good luck!

旧竹 2024-11-12 16:48:32

以下两行缺少 ;

wp_enqueue_script('pwi')
wp_enqueue_script('simpletube')

Missing ; for the following two lines:

wp_enqueue_script('pwi')
wp_enqueue_script('simpletube')
余生再见 2024-11-12 16:48:32

我将使用而不是您的代码:

<?php
function add_scripts(){
  wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
  wp_enqueue_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
  wp_enqueue_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
  wp_enqueue_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
  wp_enqueue_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
  wp_enqueue_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
  wp_enqueue_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
  wp_enqueue_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js'); 
}

add_action('wp_enqueue_scripts', 'add_scripts');

所以请注意,我已经删除了“wp_register_script”,因为如果您要在注册后立即调用 wp_enqueue ,则完全没有必要使用它。

wp_register_script

使用它是为了以后您可以在代码中的任何其他地方调用它,而不包含路径。

还有一个很大的变化是我不是从 调用该函数

init

但我是从 调用它

wp_enqueue_scripts

另外请考虑向您的 wp_enqueue_script 添加其他参数,例如

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

Instead of your code I would use:

<?php
function add_scripts(){
  wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
  wp_enqueue_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
  wp_enqueue_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
  wp_enqueue_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
  wp_enqueue_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
  wp_enqueue_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
  wp_enqueue_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
  wp_enqueue_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js'); 
}

add_action('wp_enqueue_scripts', 'add_scripts');

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.

wp_register_script

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

init

But I'm calling it from

wp_enqueue_scripts

Also please consider adding additional parameters to your wp_enqueue_script such as

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文