Stylesheet Directory Link Tag

One difficulty of designing WordPress themes you plan to let others use, is linking to images within the template directory (where the stylesheet is). Fortunately, WordPress provides a tag for doing just that.

<?php echo(get_bloginfo('template_directory')); ?>

/images/ is the images folder inside your theme folder. You can link to a page in that folder (comments.php) or something inside the images folder:

<img src="<?php echo(get_bloginfo('template_directory')); ?>/images/rss_icon.gif" alt="" />

We can make linking to an image even easier:

<?php
$bloginfo_link = get_bloginfo('template_directory');
$image_link = $bloginfo_link . '/images';
?>

<img src="<?php echo $image_link; ?>/rss_icon.gif" alt="" />

We assign the template directory path to the variable $bloginfo_link and then put the directory path and the image folder path together.

5 Responses to “Stylesheet Directory Link Tag”

Hafiz Rahman
Posted on February 1st, 2008 at 3:05 am

$bloginfo should be $bloginfo_link on the last example, but otherwise a good post. Cheer! :)

Michael Castilla
Posted on February 1st, 2008 at 7:08 am

@Hafiz: Oh thanks, I didn’t realize that. Is it fixed now?

Pierre K.
Posted on February 1st, 2008 at 2:42 pm

It’s always a good idea to use such variables in order to minimize the accesses to the mySQL database. It will improve the performance of your blog.

Elite By Design
Posted on February 6th, 2008 at 8:53 am

Very helpful Mike, especially for someone like me. Thanks for the post!

mark
Posted on June 26th, 2008 at 1:29 am

pardon my ignorance, but where do you place the php variable that sets the stage for the img code to work?

Can’t seem to get this to work.

cheers
mark

Leave a Reply