如何在wordpress中获取图像路径

Lin*_*P D 27 php wordpress

我开发了一个wordpress主题.我在'images'文件夹中有很多图像.但是当我在浏览器中抓取页面时它不会出现

我的代码是

的index.php

<ul>
<li><a href="#"><img src="images/mindset.jpg" width="145" height="32" /></a></li>
Run Code Online (Sandbox Code Playgroud)

在wordpress中有没有获取图像路径的功能?

Sad*_*dat 55

src="<?php echo base_url()?>your_theme_dir/image_dir/img.ext"
Run Code Online (Sandbox Code Playgroud)

同样

src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"
Run Code Online (Sandbox Code Playgroud)

  • bloginfo正在与我合作,但base_url无法正常工作.得了胎儿错误.. (4认同)

Ian*_*Ian 8

我必须使用样式表目录来为我工作。

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/image.png">
Run Code Online (Sandbox Code Playgroud)

  • 您可能使用儿童主题。此函数指向激活主题(您的子主题)的样式表目录,而“get_template_directory_uri();”将指向父主题目录(https://developer.wordpress.org/reference/functions/get_template_directory_uri/#comment -1797),在这种情况下这将是错误的路径。但你的答案现在应该已经被接受了。 (2认同)

小智 7

get_template_directory_uri();

此功能将帮助您检索主题目录URI,并且可以与图像一起使用,例如以下示例:

<img src="<?php echo get_template_directory_uri(); ?>/images/mindset.jpg" />
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用子主题,则此示例将不起作用。 (2认同)

Hit*_*ahu 6

你需要使用

 <?php bloginfo('template_directory'); ?>
Run Code Online (Sandbox Code Playgroud)

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

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

 themes
   |-> your theme
          |-> images
                |->subfolder
                      |-> examples.jpg
Run Code Online (Sandbox Code Playgroud)

你像这样访问它

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