• 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:

    Posted February 1, 2008

7 comments

  1. Hafiz Rahman said:

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

    on February 1, 2008 at 3:05 am Reply

  2. Michael Castilla said:

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

    on February 1, 2008 at 7:08 am Reply

  3. Pierre K. said:

    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.

    on February 1, 2008 at 2:42 pm Reply

  4. Elite By Design said:

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

    on February 6, 2008 at 8:53 am Reply

  5. mark said:

    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

    on June 26, 2008 at 1:29 am Reply

  6. dudley said:

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

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

    on September 16, 2008 at 5:51 am Reply

  7. Kim said:

    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.

    on July 8, 2010 at 3:46 pm Reply


RSS feed for comments on this post.

Leave a comment

Trackbacks on this post

    TrackBack URL