1

I'm currently trying to make a website on WordPress locally using XAMPP, I have started a website but i can't make the images display like i do when developing on a host (this is my first local WP development)

Normally, inside wp-content i make a folder called imgs and then in my code i just use

src="/wp-content/imgs/whatever.png"

but this isn't working. When i inspect the element in google chrome the folder doesn't even exist. I've tried looking at the permalinks, as well as some .htaccess stuff but to no avail. Any help to get my images working would be great

1 Answer 1

0

When you use a relative path like /some/path/here.jpg, and you are on www.example.com, the browser will look for

www.example.com/some/path/here.jpg

Which doesn't exist. But you might have noticed that you can use relative path in a CSS file. That's because the path is calculated based on the location of the CSS file, while in your case the images are being calculated based on the location of the current URL.

The main CSS file of your theme is located at your-theme/style.css. You can build relative paths within your stylesheet, for example you can use ../imgs/whatever.jpg in your CSS file to point to wp-content/imgs/whatever.jpg. However, storing any file outside upload directory or theme's is not recommended.

What you can do, is to use core functions to create a full dynamic path. For example get_template_directory_uri(); will get the current theme's path, so you can use in your image's src:

src="<?php echo get_template_directory_uri();?> /imgs/whatever.png"

To get the wp-content's URL, you can use content_url();.

4
  • Brilliant, that is exactly what i needed for the HTML side. Thanks for your help. What do i do in the case of CSS file
    – AndyGUY
    Commented Aug 12, 2017 at 14:16
  • You're welcome! WordPress's main CSS file is located at the root of theme's folder, so you should create your links relatively to that folder. For example, you can use ../imgs/whatever.jpg in your CSS file to point to wp-content/imgs/whatever.jpg. The ../ goes back once, and then navigates to imgs/
    – Johansson
    Commented Aug 12, 2017 at 14:19
  • Thank you v much, and can this be repeated as needed? ../../../imgs for example?
    – AndyGUY
    Commented Aug 12, 2017 at 14:21
  • Yes for sure. But It's not recommended to store ANY file outside your theme or upload directory, unless there is a very strong reason to.
    – Johansson
    Commented Aug 12, 2017 at 14:23

Your Answer

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

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