Img Src File path issue

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:

Read More
<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?

Related posts

3 comments

  1. 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.

  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.

  3. 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

Comments are closed.