Stylesheet Directory Link Tag

9 Comments

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.

9 thoughts on “Stylesheet Directory Link Tag

  1. 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.

  2. 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

  3. This code isn;t working properly for me. Seem like you can’t put php into a img src url.. I’m trying to post an image located in one of my theme directories but I haven’t found a way that works.

  4. My gosh, I wish I found this earlier! I just used this trick (the PHP) to replace only using /img/wejlkfsdf.jpg to finally get an image to show. Wonderful!

Comments are closed.