js脚本文件中的WordPress路径url

发布于 2024-10-20 10:54:49 字数 661 浏览 1 评论 0原文

我添加了自定义脚本:

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

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

发布评论

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

评论(6

夏末染殇 2024-10-27 10:54:49

根据 WordPress 文档,您应该在您的文件中使用 wp_localize_script()函数.php 文件。这将在标头中创建一个 Javascript 对象,该对象将在运行时可供您的脚本使用。

请参阅 Codex

示例:

<?php wp_localize_script('mylib', 'WPURLS', array( 'siteurl' => get_option('siteurl') )); ?>

要在 Javascript 中访问此变量,您只需执行以下操作:

<script type="text/javascript">
    var url = WPURLS.siteurl;
</script>

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:

<?php wp_localize_script('mylib', 'WPURLS', array( 'siteurl' => get_option('siteurl') )); ?>

To access this variable within in Javascript, you would simply do:

<script type="text/javascript">
    var url = WPURLS.siteurl;
</script>
听,心雨的声音 2024-10-27 10:54:49

在调用 wp_head() 之前,您可以通过在模板标头中设置 JS 变量来避免硬编码完整路径,并保存模板 URL。像:

<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>

并使用该变量来设置背景(我知道你知道如何做到这一点,我只包含这些详细信息,以防它们帮助其他人):

Reset.style.background = " url('"+templateUrl+"/images/searchfield_clear.png') ";

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:

<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>

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):

Reset.style.background = " url('"+templateUrl+"/images/searchfield_clear.png') ";
睡美人的小仙女 2024-10-27 10:54:49
    wp_register_script('custom-js',WP_PLUGIN_URL.'/PLUGIN_NAME/js/custom.js',array(),NULL,true);
    wp_enqueue_script('custom-js');

    $wnm_custom = array( 'template_url' => get_bloginfo('template_url') );
    wp_localize_script( 'custom-js', 'wnm_custom', $wnm_custom );

并在 custom.js 中

alert(wnm_custom.template_url);
    wp_register_script('custom-js',WP_PLUGIN_URL.'/PLUGIN_NAME/js/custom.js',array(),NULL,true);
    wp_enqueue_script('custom-js');

    $wnm_custom = array( 'template_url' => get_bloginfo('template_url') );
    wp_localize_script( 'custom-js', 'wnm_custom', $wnm_custom );

and in custom.js

alert(wnm_custom.template_url);
忆沫 2024-10-27 10:54:49

如果 javascript 文件是从管理仪表板加载的,则此 javascript 函数将为您提供 WordPress 安装的根目录。当我构建需要从管理仪表板发出 ajax 请求的插件时,我经常使用它。

function getHomeUrl() {
  var href = window.location.href;
  var index = href.indexOf('/wp-admin');
  var homeUrl = href.substring(0, index);
  return homeUrl;
}

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.

function getHomeUrl() {
  var href = window.location.href;
  var index = href.indexOf('/wp-admin');
  var homeUrl = href.substring(0, index);
  return homeUrl;
}
2024-10-27 10:54:49

适用于使用 Genesis 框架的用户。

将以下内容添加到您的子主题 functions.php

add_action( 'genesis_before', 'script_urls' );

function script_urls() {
?>
    <script type="text/javascript">
     var stylesheetDir = '<?= get_bloginfo("stylesheet_directory"); ?>';
    </script>
<?php
}

并使用该变量在脚本中设置相对 url。例如:

Reset.style.background = " url('"+stylesheetDir+"/images/searchfield_clear.png') ";

For users working with the Genesis framework.

Add the following to your child theme functions.php

add_action( 'genesis_before', 'script_urls' );

function script_urls() {
?>
    <script type="text/javascript">
     var stylesheetDir = '<?= get_bloginfo("stylesheet_directory"); ?>';
    </script>
<?php
}

And use that variable to set the relative url in your script. For example:

Reset.style.background = " url('"+stylesheetDir+"/images/searchfield_clear.png') ";
往事随风而去 2024-10-27 10:54:49

我已经使用这种方法让它工作了...

1:将回调函数添加到标头中

function custom_header() {
// Define siteURL for JS
echo '<script>var url = "'.home_url().'";</script>';
}
add_action( 'wp_head', 'custom_header' );

2:在任何所需的 JS 文件中调用 URL,如下所示:

$.infinitescroll.defaults = {
      loading: {
        msgText: 'Loading...',
        img: "" + url + "/path_to_folder/loader.gif",
      }

希望它可以帮助任何想要包含站点 URL 的人在内部 JS 或 CSS 文件中。

I've gotten it to work by using this method...

1: Adding the callback function into the header

function custom_header() {
// Define siteURL for JS
echo '<script>var url = "'.home_url().'";</script>';
}
add_action( 'wp_head', 'custom_header' );

2: Call the URL inside any desired JS file as follows:

$.infinitescroll.defaults = {
      loading: {
        msgText: 'Loading...',
        img: "" + url + "/path_to_folder/loader.gif",
      }

Hope it can help anyone looking to include a site URL in inner JS or CSS files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文