WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a plugin which sets the thumbail for your fb share image. The src is

<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>

Which was working fine, but not it's showing thumb X on every page (rather then the currect thumbnail) I found the problem is I have a loop in the sidebar.

Is there a way to make

<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>

Get its attachment from the FIRST loop (as this code is at the bottom of the page)

Or is there some other hack to make it get it from the first loop?

share|improve this question
echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); Is the code,sorry! – user1110237 Jan 11 '12 at 22:07
Write code between backcomments (this character: `) – dunc Jan 11 '12 at 22:10

Set a variable in the first loop like:

$saved_post_id = $post->ID;

Then later down the page use that variable like:

echo wp_get_attachment_url( get_post_thumbnail_id( $saved_post_id ) );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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