Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

WordPress- Get Image Path Relevant To Theme

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'); ?>

19 Sep 2008 / 52 Comments / Wordpress / by Maruf

52 Comments

  1. Nicholas Reed
    25/09/2008
    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.

  2. Maruf
    25/09/2008
    2

    Hey,

    Glad I could help.

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

  3. Debbie
    16/10/2008
    3

    Thank you! That is soooo helpful.

  4. Rhianon
    26/10/2008
    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.

  5. Long Hoang
    16/12/2008
    5

    Thanks!

  6. Steve
    18/02/2009
    6

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

  7. carlosvm
    23/03/2009
    7

    Thanks!!

  8. andiszek
    01/04/2009
    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.

  9. cynthia
    07/04/2009
    9

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

  10. Rizwan
    02/07/2009
    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.

  11. Jon
    17/07/2009
    11

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

  12. Oliver
    21/07/2009
    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)

  13. bulvadesign.com
    06/08/2009
    13

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

  14. arnaud
    31/08/2009
    14

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

  15. Référenceur Freelance
    17/09/2009
    15

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

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

  16. cristian
    13/10/2009
    16

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

  17. Letsnurture
    16/10/2009
    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

  18. Gary
    18/11/2009
    18

    Thanks a lot mate. You made my day.

  19. wes
    19/11/2009
    19

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

  20. Alan
    10/01/2010
    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.

  21. Andy
    12/01/2010
    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!

  22. Carole Hayes
    27/01/2010
    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??

  23. Carole Hayes
    27/01/2010
    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! : )

  24. Vanessa
    29/01/2010
    24

    Thank u! u save my life!

  25. Karlos
    04/03/2010
    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! ;)

  26. RiaanWest
    09/03/2010
    26

    Thanks for this man, helped me a lot!

  27. Daniel
    20/05/2010
    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!

  28. Dave
    07/06/2010
    28

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

  29. Rhys
    04/07/2010
    29

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

  30. daniel
    06/07/2010
    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.

  31. Mazy
    17/07/2010
    31

    Love you man!

  32. Solutions in Design
    20/07/2010
    32

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

  33. Jared
    22/07/2010
    33

    Thank you for the clear and simple solution sir.

  34. No
    31/07/2010
    34

    Just what I was looking for :) Thx !

  35. HipHopZen
    06/08/2010
    35

    Thank you!

    Have a good karma.

  36. jesuda
    21/08/2010
    36

    Thanks dude.. very quick fix

  37. Fernanda N
    24/08/2010
    37

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

  38. 15/12/2010
    38

    Ahhh, better than advil. Thanks for relieving my pain!

  39. Dan
    20/12/2010
    39

    Thanks a lot! This is exactly what I was looking for.

  40. HDDesigns
    31/12/2010
    40

    Not working in stylesheet file.

    body {
    background-image:url(“/images/bodybg1.png” title=”" alt=”");
    background-repeat: repeat-x;
    background-color:#1f1f1f;
    }

    Any help will be appreciated…
    Thanks

  41. Maruf
    04/01/2011
    41

    Hey HDDesigns,

    Remove the “title” and “alt” tags from the background-image property. They’re not valid!

    So it should be:
    background-image:url(“/images/bodybg1.png”);

    Regards

  42. 42

    thank you for this code.

  43. rafat
    17/01/2011
    43

    Thks a Ton dude

  44. 08/02/2011
    44

    THANK YOU x 1,000,000,000,000! I’m new to working in WP and it has been an uphill climb

  45. Tracy
    17/03/2011
    45

    oh my god i love you! i have been pulling my hair out for hours trying to figure this out!

  46. Kristy (Malaysia)
    04/04/2011
    46

    Thank you! I was searching for this and it really helps!!

  47. Kristy (Malaysia)
    04/04/2011
    47

    Oh, I hit a road block. I am using a twentytenchild template and have customised the loop.php. But no matter what I do, bloginfo(‘template_directory’) displays the main twentyten url for images. I don’t get it cause the other files work fine. Any ideas?

  48. 19/04/2011
    48

    Exactly what I needed. Thanks a lot for the info!

  49. 19/04/2011
    49

    If you’re working with child themes you must change ‘template_directory’ to ‘stylesheet_directory’.

  50. Gregg
    04/07/2011
    50

    Thanks so much! Much head-to-desk thumping avoided!

  51. 30/09/2011
    51

    i spent the whole day finding for answer! really it took me all day! thanks to you i found the answer!… i dont know the keyword to use to search the answer … magically i thought of searching wordpress image path.. and then i found your blog… im very relieved

  52. Vasim Padhiyar
    01/11/2011
    52

    Thanks mate

Leave a Reply

© 2012 BrightCherry :)