halliewuud
halliewuud

Reputation: 2785

wordpress image path changing dependent on file

I'm having problems with the paths to my first wordpress theme. Post images and stuff not related to css is located in wordpress_folder/blog-images/ and in index.php when I link to images I use this path: blog-images/img.jpg

The problem is now that when I want to link to the same image from another file (not index.php) in this case single.php wich displays one blog post, the correct path is now ../../../blog-images/img.jpg

This is causing problems in the includes like sidebar etc. sidebar.php works fine when called from index.php but the images path is changed if sidebar.php is called from single.php.

Does anyone know what's going on?

Upvotes: 0

Views: 750

Answers (2)

mrtsherman
mrtsherman

Reputation: 39872

I thought this was a little unclear from drew's answer, so I am adding a little bit more in a separate answer. His advice is sound and I agree with him.

If you prepend a url with a / then it will navigate based on your site url. Without the slash it uses relative navigation.

So here are some examples for www.mydomain.com

//always shows the image located at http://www.mydomain.com/myfolder/pic.png
//no matter what the url is
<img src="/myfolder/pic.png" />

//shows the image located relative to the current path
//if current url is http://www.mydomain.com/posts/ then the image will come from
//http://www.mydomain.com/posts/myfolder/pic.png
<img src="myfolder/pic.png" />

If you are creating links dynamically from php side then you will want to use site_url().

If you are creating links to your theme directory folder then you will want to use bloginfo('template_directory')

Upvotes: 1

drew010
drew010

Reputation: 69937

If you are creating these links from within php scripts, I would suggest using the site_url() function to get the URL for your wordpress install and then appending your images path to the end of that. If you are editing static theme files like css, then you should use /wordpress_folder/blog_images/img.jpg.

Something like <img src="<?php echo site_url() ?>/blog_images/img.jpg" /> should be sufficient from theme files.

The reason that paths are chaning is because if you are in wordpress_folder then the path blog_images/img.jpg resolves to wordpress_folder/blog_images/img.jpg but if you are on a post that has the url yoursite.com/wordpress_folder/2011/09/category/my_great_post then the path would resolve to wordpress_folder/2011/09/category/blog_images/img.jpg which is obviously incorrect.

For this reason you should try to use the absolute path or full URL so that no matter what file/folder/url you are linking from, the path will always be correct.

The main downside you may run into is that if you were to change the name of your wordpress folder, or remove it altogether, then you may need to make a lot of edits to reflect that. But in any case, you should put the / in front of your path so that it can be referenced the same from everywhere.

Also check out the site_url() reference page, it lists some other helpful functions at the bottom that may be useful to you.

Upvotes: 3

Related Questions