Mat*_*cer 9 css wordpress stylesheet wordpress-plugin
我正在为wordpress编写一个插件,但是我遇到了图像问题.如果我的插件位于wp-content/plugins/my-plugin /并且在那里,文件夹images/test.png - 如何在我的代码中引用该图像?我不想将图像放到主题中,因为当其他用户来获取我的插件时,图像将无法正常工作!
所以我的结构是
myplugin/plugin.php (which includes several files...)
myplugin/pluginstyle.css
myplugin/includes/page.php
myplugin/images/test.png
Run Code Online (Sandbox Code Playgroud)
我的样式表工作得很好,但是当我尝试使用图像作为元素的背景时,它不起作用.
如何在插件中引用图像?
测试page.php的输出
<div class="test"><p>hello</p></div>
Run Code Online (Sandbox Code Playgroud)
CSS
.test { background: url(../images/test.png) repeat-x; }
Run Code Online (Sandbox Code Playgroud)
我哪里错了?有没有我应该使用的方法?谢谢你的帮助!
WordPress的PHP常量WP_PLUGIN_URL包含插件文件夹的绝对URL.所以,要获取网址,请使用WP_PLUGIN_URL . '/myplugin/images/test.png'.在样式表中,图像路径始终相对于样式表本身.运用
.test { background: url(images/test.png); }
Run Code Online (Sandbox Code Playgroud)
应该工作,只要它在外部样式表中.如果是内联的,则应使用绝对URL.
一种方法是:
plugins_url('images/test.png', __FILE__)
Run Code Online (Sandbox Code Playgroud)
...这将为您提供正确的URL,即使用户更改了父目录的名称也是如此。