How can I get the image url in a Wordpress theme?

Viewed 179320

I am developing a theme for wordpress. And I have many images in 'images' folder. But when I take the page in browser it is not comming.

My code is

index.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 ?

9 Answers

get_template_directory_uri();

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

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

I had to use Stylesheet directory to work for me.

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

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)

I strongly recommend the following:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/img-folder/your_image.jpg">

It works for almost any file you want to add to your wordpress project, be it image or CSS.

If your img folder is inside your theme folder, just follow the example below:

<img src="<?php echo  get_theme_file_uri(); ?>/img/yourimagename.jpg" class="story-img" alt="your alt text">
Related