php 如何在wordpress中获取图片网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3262925/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:09:56  来源:igfitidea点击:

How can i get the image url in wordpress

phpwordpress

提问by Linto P D

Iam developing a theme for wordpress.And i have many images in 'images' folder.But when i take the page in browser its not comming

我正在为 wordpress 开发一个主题。我在“图像”文件夹中有很多图像。但是当我在浏览器中获取页面时,它不会出现

My code is

我的代码是

index.php

索引.php

<ul>
<li><a href="#"><img src="images/mindset.jpg" width="145" height="32" /></a></li>

Is there any function for getting the image path in wordpress ?

是否有任何功能可以在 wordpress 中获取图像路径?

回答by Sadat

src="<?php echo base_url()?>your_theme_dir/image_dir/img.ext"

As well

同样

src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"

回答by Allen

get_template_directory_uri();

This function will help you retrieve theme directory URI, and can be used with your images, your example below:

get_template_directory_uri();

此函数将帮助您检索主题目录 URI,并可与您的图像一起使用,您的示例如下:

<img src="<?php echo get_template_directory_uri(); ?>/images/mindset.jpg" />

回答by Ian

I had to use Stylesheet directory to work for me.

我不得不使用样式表目录来为我工作。

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/image.png">

回答by Hitesh Sahu

You need to use

你需要使用

 <?php bloginfo('template_directory'); ?>

It returns the directory of the current WordPress theme.

它返回当前 WordPress 主题的目录。

Now for example, if your theme has some image named example.jpginside some folder name subfolderfolder in the imagesfolder.

现在例如,如果您的主题example.jpg在文件subfolder夹中的某个文件夹名称文件夹中命名了一些图像images

 themes
   |-> your theme
          |-> images
                |->subfolder
                      |-> examples.jpg

You access it like this

你像这样访问它

 <img class="article-image" src="<?php bloginfo('template_directory'); ?> /images/subfolder/example.jpg" border="0" alt="">

回答by Bikram Shrestha

src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"
worked for me for wordpress 3.6 i used it to get header logo using as

src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"
为我工作的 wordpress 3.6 我用它来获取标题标志使用

<img src="<?php bloginfo('template_url'); ?>/images/logo.jpg">

回答by Owais Alam

You can also define image path in following way

您还可以通过以下方式定义图像路径

<li><a href="#"><img src="/wp-content/theme-name/images/mindset.jpg" width="145" height="32" /></a></li>

回答by Munesu Dube

If you are developing a child theme you can use:

如果您正在开发一个子主题,您可以使用:

<img src="<?php echo get_template_directory_uri(); ?>-child/images/example.png" />

get_template_directory_uri()will return url to your currently active theme (parent theme), then you add -child/, then add path to your image (the example above assumes your image is at <child-theme-directory>/images/example.png)

get_template_directory_uri()会将 url 返回到您当前活动的主题(父主题),然后添加 -child/,然后添加图像的路径(上面的示例假设您的图像位于<child-theme-directory>/images/example.png

回答by Javapocalypse

You asked of Functionbut there is an easier way too. When you will upload the image, copy it's URL which is at the top right part of the window (View Screenshot) and paste it in the src='[link you copied]'. Hope this will help if someone is looking for similar problem.

您询问了Function ,但也有更简单的方法。当您上传图像时,复制其位于窗口右上角的 URL(查看屏幕截图)并将其粘贴到src='[link you copied]'. 如果有人正在寻找类似的问题,希望这会有所帮助。