I have this function call that displays an entire img tag with the correct url for that post.. the thumbnail img...

?php the_post_thumbnail('blog-page'); ?>

What I need is just the source address for that img.

How can I get that source dynamically?

Here is a little more explanation of what it is exaclty that I'm doing...

This is the code that displays the thumbnail for a page in my wordpress:

<div class="entry-thumbnail">
<a rel="lightbox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-page'); ?></a>

I need that a tag's href to use the img's source instead of linking to the blog post it's self to get lightbox to work properly.

2 Answers 2

wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

all together:

    <div class="entry-thumbnail">
       <a rel="lightbox" href="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>"><?php echo get_the_post_thumbnail( $post->ID ); ?></a>
  • the href returns blank with this.
    – ninja08
    Commented Jul 20, 2013 at 0:49
  • are you using it in the loop? it works just fine for me.
    – Vigs
    Commented Jul 20, 2013 at 0:55
  • I'm sure it's in the loop, otherwise the rest of the content wouldn't show, right?
    – ninja08
    Commented Jul 20, 2013 at 1:14
  • G.M. pointed out a error below, it should work now.
    – Vigs
    Commented Jul 20, 2013 at 3:06

In your code you use the_post_thumbnail('blog-page') where 'blog-page' is a custom image size previous registered with add_image_size. So, if you want control thumbnail size, try this:

global $post;
$imageid = get_post_thumbnail_id( $post->ID );
$image_thumb = wp_get_attachment_image_src( $imageid, 'blog-page' );
$image_url = wp_get_attachment_url( $imageid );
if you want to control image size also for lightbox (useful for very big images) use instead:
$image_large = wp_get_attachment_image_src( $imageid, 'large' );
$image_url = $image_large[0];

<div class="entry-thumbnail">
   <a rel="lightbox" href="<?php echo $image_url; ?>">
       <img src="<?php echo $image_thumb[0]; ?>" alt="<?php the_title(); ?>" />
  • All I want is the source address for the thumbnail. I'd think that Ron's code should work, since I can see how I'd construct the same code with what you've written here. Thanks for the extra advice though, I can see this being useful later on.
    – ninja08
    Commented Jul 20, 2013 at 1:27
  • wp_get_attachment_url return the url of the full image, not of the thumbnail. if you want the url of the thumbnail you have to use wp_get_attachment_image_src.
    – gmazzap
    Commented Jul 20, 2013 at 2:06
  • 1
    Ron's code probably don't work because echo is missing inside href attribute, before wp_get_attachment_url
    – gmazzap
    Commented Jul 20, 2013 at 2:11
  • good catch. I had fixed that on my local host and fergot to update the answer
    – Vigs
    Commented Jul 20, 2013 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.