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”
As a result, you’re left with something like this:
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:
<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
<img src="<?php bloginfo('template_directory'); ?>/images/mainimage.jpg" title="" alt="" />
Yup, it’s as easy as that.
All you need is
<?php bloginfo('template_directory'); ?>