WordPress- Get Image Path Relevant To Theme

Image frame
WordPress- Get Image Path Relevant To Theme
Maruf
Sep 19th, 2008
Maruf scribbled this post.

I’ve had a few people ask me how they can link an image into a theme. Apparently they’re having problems finding the relevant path, no matter how many directories they go in and out of E.g “../../../images/mainimage.jpg”

The Problem

As a result, you’re left with something like this:

Missing image

It sounds like an unbelievably simple problem, but I think it’s it’s actually quite a common hurdle for many people who are just starting to develop WordPress themes. So for example, let’s say you’re creating a theme, and you want to display a logo. First you’d upload the image into the image folder, and then you’d type something like this:

1
<img src="blog/wp-contents/themes/mytheme/images/mainimage.jpg" title="" alt=""

Of course, if you try that path, it won’t work, no matter how many directories you escape. The problem is that the WordPress htaccess file is redirecting the directory and making it impossible for you to get the file.

The solution is simple

1
<img src="<?php bloginfo('template_directory'); ?>/images/mainimage.jpg" title="" alt="" />

Yup, it’s as easy as that.

All you need is

1
<?php bloginfo('template_directory'); ?>

Filed away: Wordpress

comments

Image frame
1

Hey man, you just saved me, I’ve just started developing a theme and this is exactly what I needed, spent about 30 minutes on it!
thanks dude.

is that you in the picture, your pretty good looking.

Nicholas Reed
Sep 25th, 2008
Image frame
2

Hey,

Glad I could help.

And yup, that’s me! Thank you very much! :)

Maruf
Sep 25th, 2008
Image frame
3

Thank you! That is soooo helpful.

Debbie
Oct 16th, 2008
Image frame
4

THANK GOD. I spent 2 days googling for a way to solve this (and i’m usually pretty good at google-fu). THANK YOU.

Rhianon
Oct 26th, 2008
Image frame
5

Thanks!

Long Hoang
Dec 16th, 2008
Image frame
6

Thanks – I love it when people post little, simple things like this, amazingly helpful.

Steve
Feb 18th, 2009
Image frame
7

Thanks!!

carlosvm
Mar 23rd, 2009
Image frame
8

thanks a lot, your tip just saved me. i was playing around with my first WP project on Localhost and the images would not show up.

andiszek
Apr 1st, 2009
Image frame
9

So awesome! I was having issues with things not showing up. Will definitely keep a bookmark of this, lol.

cynthia
Apr 7th, 2009
Image frame
10

I was getting frustrated with images not showing up in certain pages. Now I know what the correct way to reference an image is. Thanks.

Rizwan
Jul 2nd, 2009
Image frame
11

I stumbled on this while doing subpages, and almost ended up using not-so-nice absolute urls. Thanks a bunch on his tip!

Jon
Jul 17th, 2009
Image frame
12

Thanks for the tip!

One thing though, how would I link to images in the theme files which were outside of the theme directory?

for example an image which is at

/images/someimage.png

With the url:
http://www.example.com/images/someimage.png)

Oliver
Jul 21st, 2009
Image frame
13

Thx a lot man. Very helpful for WP newbee like I am.

bulvadesign.com
Aug 6th, 2009
Image frame
14

Try : folder_image();
it works better ;o)

arnaud
Aug 31st, 2009
Image frame
15

Thanks Maruf, just writting my first theme and this saved me some digging around.

Arnaud, your solution doesn’t work for me :)

Référenceur Freelance
Sep 17th, 2009
Image frame
16

i really really love your post. you saved me a lot of time. i might have never figured this out.

cristian
Oct 13th, 2009
Image frame
17

Hi great solution.

I’m fledgling to word press. And what all I’m doing is customizing a new theme.

Till now i had been using the static path to my current theme everywhere.

Thanks,
Sachin Pethani
http://www.letsnurture.com

Letsnurture
Oct 16th, 2009
Image frame
18

Thanks a lot mate. You made my day.

Gary
Nov 18th, 2009
Image frame
19

Like many of these posters, I thank you. Keep up the good work!

wes
Nov 19th, 2009
Image frame
20

Thanks so much. I spent hours a few weeks ago trying to figure it out and eventually just started using empty divs with CSS images and accessing all of my html images through the website URL. ha! Newbies.

Alan
Jan 10th, 2010
Image frame
21

Man, you dont know how much time it took me to find this information on the net. I couldnt figure out the he** was going on, i appreciate the post, good stuff man!

Andy
Jan 12th, 2010
Image frame
22

I swear, my blonde hair goes straight into the center of my brain sometimes, because this is working for everybody else, and I can’t get it to work for me!

Can you please help me figure out what I’m doing wrong??

Carole Hayes
Jan 27th, 2010
Image frame
23

Okay, my husband figured it out!

It was a punctuation error…. I think there was an extra quote or something equally stupid.

Thank you for putting this out there for those of us who are clueless about such things! : )

Carole Hayes
Jan 27th, 2010
Image frame
24

Thank u! u save my life!

Vanessa
Jan 29th, 2010
Image frame
25

Awesome awesome awesome :)

Just started re-designing my blog, decided to go for a completely new template build, rather than template “hack” – this simple trick saved me heaps!

LEGEND! ;)

Karlos
Mar 4th, 2010
Image frame
26

Thanks for this man, helped me a lot!

RiaanWest
Mar 9th, 2010
Image frame
27

Any idea what to do for swap out mouseover images? (onmouseout=”MM_swapImgRestore()”) Obviously the java doesn’t like the PHP call. Can I just not use a swapImgRestore in WordPress? Is there a better way to do pre-loading (cached) mouseover in WordPress? Thanks for your tips and help!

Daniel
May 20th, 2010
Image frame
28

This just saved me a lot of head scratching. Thanks!

Dave
Jun 7th, 2010
Image frame
29

spent way too long tring to figure this one out! Nice post, thanks alot!

Rhys
Jul 4th, 2010
Image frame
30

Any ideas on the java/mouseover thing? Dreamweaver utilizes a java script for mouseover events. The PHP path won’t work in a Java script, so you can’t have mouseover events in WordPress using the method you suggest.

daniel
Jul 6th, 2010
Image frame
31

Love you man!

Mazy
Jul 17th, 2010
Image frame
32

Hey thanks a lot man!!! Nice site design by the way.

Solutions in Design
Jul 20th, 2010
Image frame
33

Thank you for the clear and simple solution sir.

Jared
Jul 22nd, 2010
Image frame
34

Just what I was looking for :) Thx !

No
Jul 31st, 2010
Image frame
35

Thank you!

Have a good karma.

HipHopZen
Aug 6th, 2010
Image frame
36

Thanks dude.. very quick fix

jesuda
Aug 21st, 2010
Image frame
37

thank you so much!
i’ve been looking for this code for days and finally found it here! thanks for the tip! =D

Fernanda N
Aug 24th, 2010

feel free to leave a scribble

Name:
Email:
gravatar
Want an image next to your comments?
visit gravatar.com
Message:
Get a free quote