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'); ?>
Learning & sharing
This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.
Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.