4

To get images loaded correctly when I use img src in HTML, I have to give the entire file path. Using CSS I would only need to use:

`background-image: url(images/morebutton.png)`

With img src I have to use this:

<img src="wp-content/themes/blankslate/images/morebutton.png">

Using this file path gives me the image on the home page, however when I click through to the article page, the image doesnt load.. I'm having the same issue with my logo, it shows on the homepage, any additional pages it doesnt show.

Can anyone tell me if the file path setup I have is incorrect?

1
  • Read about relative and absolute links. The concept is common and if you create HTML content, you should know about this. You then can use each as you see fit, but first you need to understand how it work. The URL in the CSS file for example is always relative to the CSS file location. The SRC link is always relative to the location of the requested HTML, so it can vary if you use the HTML in different locations.
    – hakre
    Aug 14, 2013 at 7:54

3 Answers 3

8

No your file path setup is correct, you need to provide the absolute path in you img src for images to load on other pages as relative path would change to,

 http://yourwebsite.com/page/wp-content/themes/blankslate/images/morebutton.png 

and instead it should be

http://yourwebsite.com/wp-content/themes/blankslate/images/morebutton.png 

So you should define a constant in your function.php for path to image directory, and then use it in img src.

  if( !defined('THEME_IMG_PATH')){
   define( 'THEME_IMG_PATH', get_stylesheet_directory_uri() . '/images' );
  }

and then you can use img tag as

 <img src="<?php echo THEME_IMG_PATH; ?>/morebutton.png" alt=""/>

That would solve your issue. You can use the constant anywhere in your theme, handy to use.

3
  • You can also adopt the relative paths. In the end, the browser will create an absolute link out of it anyway. The most important thing is that the browser created absolute link is correct. Knowing how the browser constructs that link is important, the rules are in 5. Reference Resolution (RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax). I'd say it's worth to know about when you're creating HTML.
    – hakre
    Aug 14, 2013 at 7:55
  • According to the documentation you cannot put php into a wordpress page. Where exactly are you putting this code? Mar 21, 2020 at 19:17
  • This is being added in theme's file.
    – Kumar
    Mar 22, 2020 at 3:01
2

Try this:

<img src="<?php echo get_bloginfo( 'template_directory' ); ?>/images/morebutton.png" />

I typically let WordPress direct traffic as much as possible to avoid any conflicts. It's definitely easier as well to always look to the predefined functions that have been built into the core.

1
  • I'm uploading the files directly, if I had a little more knowledge with php I would attempt this, the above answer worked for me! Appreciate the reply Aug 14, 2013 at 7:36
1

You can use this relative path:

<img src='/wp-content/themes/blankslate/images/morebutton.png'>

The / before the path tells the browser to go to the root directory and search for the wp-content folder and go from there.

Alternatively you can always use the absolute url path in img src. Like

 <img src='http://yourdomain.com/wp-content/themes/blankslate/images/morebutton.png'>

see also here how to move up or down the initial folders of your relative paths

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.