php 如何正确使用 get_template_directory_uri() WordPress 函数加载我的主题子文件夹中的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21258133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly use get_template_directory_uri() WordPress function to load an image that is in a subfolder of my theme?
提问by AndreaNobili
I am pretty new in WordPress and I have the following doubt about how to insert in my homepage an immage that is in a subfolder into my theme directory.
我是 WordPress 的新手,我对如何将子文件夹中的图像插入到我的主题目录中的主页有以下疑问。
So I have the following situation: Into my custom theme directory I have the following folder that contains a jpg immage: /assets/img/flexslider/flex-1.jpg
所以我有以下情况:在我的自定义主题目录中,我有以下包含 jpg 图像的文件夹:/assets/img/flexslider/flex-1.jpg
Now in my header.phpfile I have something like this:
现在在我的header.php文件中,我有这样的东西:
<li>
<img src="assets/img/flexslider/flex-1.jpg">
<div class="flex-caption">
<p class="flex-caption-text">
<span>Lorem ipsum</span><br>
<span>sit dolor</span><br>
<span>adipiscing elitur</span>
</p>
</div>
</li>
Obviously, when I load the page, the immage flex-1.jpgis not loaded because there is not the right path (infact using FireBug I obtain that it try to load the assets/img/flexslider/flex-1.jpgimmage) so I think that I could use the absolute path but this is pretty orrible !!!
显然,当我加载页面时,图像flex-1.jpg没有加载,因为没有正确的路径(事实上使用 FireBug 我得到它尝试加载资产/img/flexslider/flex-1.jpg图像)所以我认为我可以使用绝对路径,但这非常糟糕!!!
So I am thinking to use the get_template_directory_uri()function provided from WP to do this and I have try to change the previous code in this way:
所以我想使用WP 提供的get_template_directory_uri()函数来做到这一点,我尝试以这种方式更改以前的代码:
<li>
<img src=<?php get_template_directory_uri().'/assets/img/flexslider/flex-1.jpg' ?>>
<div class="flex-caption">
<p class="flex-caption-text">
<span>Lorem ipsum</span><br>
<span>sit dolor</span><br>
<span>adipiscing elitur</span>
</p>
</div>
But don't work and using FireBugI can see that load nothing, infact in my brower source code I have:
但是不工作并且使用FireBug我可以看到没有加载任何内容,事实上在我的浏览器源代码中我有:
<img src="">
Why don't work? What am I missing?
为什么不工作?我错过了什么?
Tnx
Tnx
Andrea
安德烈亚
回答by Anjana
I hope it will work:
我希望它会起作用:
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/flexslider/flex-1.jpg" />
If your assets folder inside theme.
如果你的assets文件夹里面的主题。
回答by Pranay Bhardwaj
please try :
请尝试 :
<img src="<?php print(get_template_directory_uri()); ?>/assets/img/flexslider/flex-1.jpg" />
just check for slash, if double before "assests", remove static one.
只需检查斜杠,如果在“assests”之前加倍,则删除静态斜杠。
回答by squarecandy
You can also use:
您还可以使用:
<img src="<?php bloginfo('template_url'); ?>/images/yourimage.jpg">

