【问题标题】:How to link to the WordPress plugins folder from style.css?如何从 style.css 链接到 WordPress 插件文件夹?
【发布时间】:2014-08-13 09:00:05
【问题描述】:

我正在尝试从 style.css 链接到 WordPress 插件文件夹中的图像。我尝试了以下方法,但不起作用:

.example {
    background: url( images/photo.jpg ) center center no-repeat;
}

插件文件夹图片: /wp-content/plugins/my-plugin/images/photo.jpg

样式表在这里: /wp-content/themes/my-theme/style.css

【问题讨论】:

  • 在您的插件文件夹中添加 css 和图像使用插件 url 获取此内容。
  • 我不希望有很多 CSS 文件

标签: css wordpress relative-path


【解决方案1】:

我知道您添加了“相对路径”标签,但您是否尝试过使用绝对路径,包括域名?

考虑尝试

.example {
    background: url('http://full-path.com/wp-content/plugins/my-plugin/images/photo.jpg') center center no-repeat;
}

如果你想使用相对路径,看起来你也可以尝试:

../../plugins/my-plugin/images/photo.jpg

这假设服务器正在从 CSS 所在的文件夹中查找以解析照片的路径。 “..”表示向上移动目录级别。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可以对资源使用所谓的半相对路径:

    .example {
        background-image: url( '/wp-content/plugins/path/to/image.jpg' );
    }
    

    上面的/wp-content/ 映射到http://www.domain.xyz/wp-content/,它允许您从图像的URL 路径中省略域部分。如果您省略开头的 / 字符,样式表将在 CSS 文件所在的目录中查找 wp-content/...

    注意:如果您的插件目录不在wp-content 之外,上述方法将不起作用。

    如果您想要使用 WordPress 函数和常量的确切插件目录路径,请考虑使用 PHP 注入 CSS:http://css-tricks.com/css-variables-with-php/。这样您就可以在 CSS 文件中执行一些 PHP,其中可能包括将 WP 插件目录获取到 PHP 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多