Kazu25
Kazu25

Reputation: 97

why does relative path is not working for <img src="""> in wordpress

I know the solution for this problem The solution is to use "get_template_directory_uri()"

But Can someone please tell me why this relative path itself "/img/entry.png" is not working even though I'm using parent theme(Not child theme)

Before(Not working)

<img src="img/entry.png" alt="">

After(Working) → Why??

 <img src="<?php echo get_template_directory_uri()?>/img/entry.png" alt="">

I putted index.php and Img folder at the same level in Theme folder as below. So why "img/entry.png" is not working?

 (Theme folder)
      |
      |--index.php
      |--img
          |-entry.png

Upvotes: 2

Views: 1807

Answers (2)

Saeed Jamali
Saeed Jamali

Reputation: 1195

the root of wordpress project is consisted od some directories such as 'wp-content' and 'wp-admin' and doesn't contain any folder named 'img'.

the 'src' attribute of images in HTML tag targets the root of the project then reads the url. so you should set your img folder path correctly from the project root.

Upvotes: 1

mahyard
mahyard

Reputation: 1248

get_template_directory_uri() adds the path of the directory in which img/is located. If you want not to add that function then you have to add the path manually.

For example if your WordPress instance uses a theme called my-theme then this /wp-content/themes/my-theme/img/my-image.jpg could be served to img's src.

Upvotes: 1

Related Questions