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'm trying to get the post thumbnail in the sidebar which is in essence outside the loop.
For some reason it seems impossible!!!
I have tried all kinds of ways.
Here is the wirking code: (I have edited the code now that I found it)

global $wp_query;
$post_id = $wp_query->post->ID;

if (has_post_thumbnail( $post_id ) ):
    $image_id = get_post_thumbnail_id($post_id);
    $imazz = wp_get_attachment_image_src($image_id,'medium',true);
    $image_url = $imazz[0];
    $image_url = '<img src="'.$image_url.'" alt="'.the_title().'" />';
else :
    php $image_url = '<img src="'.get_template_directory_uri().'/images/logo-pic-inv.jpg" width="500" height="333" alt="'.__("Δημοσιεύσεις","44db").'" />';

I have also tried using the following code:

get_the_post_thumbnail($post->ID); // using the post id

and that

get_the_post_thumbnail($post_id); // using the fetched post id

any help would be very much appreciated. Thanks

share|improve this question
I have edited the code and the initial part is correct. – ion Jul 5 '11 at 9:37
Do you want to display a Post Thumbnail in the sidebar of a Single Blog Post, or of an archive index page? – Chip Bennett Jul 5 '11 at 11:51


global $post;

if (has_post_thumbnail( $post->ID ) ){

share|improve this answer
I also like get_the_ID() template tag, more explicit and harder to make mistake with globals. – Rarst Jul 5 '11 at 12:15
But this is outside the loop, so get_the_id() wouldn't be too handy here. – Isaac Lubow Jan 24 '15 at 8:24

The problems is that you're not inside a loop when you're in the sidebar. By the time you hit the sidebar your loop has finished, so even getting $wp_query->post->ID won't work.

Try doing:


to reset the loop and then get the first entry loaded up, before the code you included.

If you wanted to be a bit better then you can do:

if (have_posts()) : the_post();

Hope that helps

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.