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.

Post Revisions:

Stay up to date on WordPress news:

About the author:

Do you have your copy of The WPCandy Quarterly?

The Quarterly is our print magazine all about WordPress. It is released four times yearly (hence the name) and is filled to the brim with exclusive articles about WordPress, written by some of the brightest minds in the community.

Pre-order your issue today!

9 Responses to Stylesheet Directory Link Tag

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

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

  3. Pierre K. says:

    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.

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

  5. mark says:

    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

  6. dudley says:

    <img src=”/images/logo.gif” alt=”logo”>

    Hi Mark, maybe its the code thats wrong, try the above.

  7. Kim says:

    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.

  8. I wish there was an image constant URL pre-defined in WP.

    I guess I will have to use this technique. Oh well.

  9. Cynthia says:

    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!

Leave a Reply

Please note that WPCandy is a moderated community.

 

Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>