18
Dynamically generating the file path to the template directory
2 Comments · Posted by Jonny in Template Development
When you start getting deeper into WordPress theme development, you will often have to reference images, includes and other files. To keep things tidy, you should keep all of these in the template folder. Then you can move your theme around different blogs and websites and all the paths will work.
You simply need the piece of code:
<?php bloginfo('template_directory'); ?>
This will generate the url to the current template folder dynamically through WordPress. For instance, you may have included some Javascript in your theme, and it’s convenient to keep this all together in a folder called ‘jscript’ within your template directory. Rather than have to hard-code the URL path in your template file, like ‘www.mysite.com/wp-content/themes/mytheme/jscript/script1.js’ you would write this as:
<?php bloginfo('template_directory'); ?>/jscript/script1.js
Armand · February 21, 2009 at 7:11 am
I would like to get the image on my template directory. Thanks for the code.
Admin comment by Jonny · February 23, 2009 at 8:07 pm
You need to create a PNG image 300px wide by 255px high called ’screenshot.png’ – this automatically gets used if you put it in your theme directory.