js脚本文件中的WordPress路径url
我添加了自定义脚本:
wp_enqueue_script('functions', get_bloginfo('template_url') . '/js/functions.js', 'search', null, false);
它工作得很好,除了在我的 functions.js
中:
Reset.style.background = "url('../images/searchfield_clear.png') no-repeat top left";
这以前可以工作,直到我更改为漂亮的永久链接和 .htaccess
文件夹层次结构如下:
themename/js themename/images
(images和js文件夹位于themename文件夹中)
我尝试过 ../images - ./image - /images
通常,无论 js 文件所在的位置,它都应该返回 1 级....
我不想使用完整路径。
WordPress 是否有另一种方法可以识别 javascript 文件以获得正确的路径?
目前我只是很困惑我做错了什么。
I added custom script:
wp_enqueue_script('functions', get_bloginfo('template_url') . '/js/functions.js', 'search', null, false);
It works great, except in the functions.js
I have:
Reset.style.background = "url('../images/searchfield_clear.png') no-repeat top left";
This used to work before, until I changed to pretty permalinks and .htaccess
The folder hierarchy is like:
themename/js themename/images
(the images and js folder are in themename folder)
I tried ../images - ./image - /images
Normally it should go back 1 level wherever the js file is located....
I don't want to use full path.
Is there another way that WordPress can recognize in the javascript file to have the correct path?
Currently I am just confused what I am doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 WordPress 文档,您应该在您的文件中使用
wp_localize_script()
函数.php 文件。这将在标头中创建一个 Javascript 对象,该对象将在运行时可供您的脚本使用。请参阅 Codex
示例:
要在 Javascript 中访问此变量,您只需执行以下操作:
According to the Wordpress documentation, you should use
wp_localize_script()
in your functions.php file. This will create a Javascript Object in the header, which will be available to your scripts at runtime.See Codex
Example:
To access this variable within in Javascript, you would simply do:
在调用
wp_head()
之前,您可以通过在模板标头中设置 JS 变量来避免硬编码完整路径,并保存模板 URL。像:并使用该变量来设置背景(我知道你知道如何做到这一点,我只包含这些详细信息,以防它们帮助其他人):
You could avoid hardcoding the full path by setting a JS variable in the header of your template, before
wp_head()
is called, holding the template URL. Like:And use that variable to set the background (I realize you know how to do this, I only include these details in case they helps others):
并在 custom.js 中
and in custom.js
如果 javascript 文件是从管理仪表板加载的,则此 javascript 函数将为您提供 WordPress 安装的根目录。当我构建需要从管理仪表板发出 ajax 请求的插件时,我经常使用它。
If the javascript file is loaded from the admin dashboard, this javascript function will give you the root of your WordPress installation. I use this a lot when I'm building plugins that need to make ajax requests from the admin dashboard.
适用于使用 Genesis 框架的用户。
将以下内容添加到您的子主题
functions.php
并使用该变量在脚本中设置相对 url。例如:
For users working with the Genesis framework.
Add the following to your child theme
functions.php
And use that variable to set the relative url in your script. For example:
我已经使用这种方法让它工作了...
1:将回调函数添加到标头中
2:在任何所需的 JS 文件中调用 URL,如下所示:
希望它可以帮助任何想要包含站点 URL 的人在内部 JS 或 CSS 文件中。
I've gotten it to work by using this method...
1: Adding the callback function into the header
2: Call the URL inside any desired JS file as follows:
Hope it can help anyone looking to include a site URL in inner JS or CSS files.