Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I call the current post's featured image in the sidebar? and if there is none, then a backup image?

share|improve this question
    
0% accept rate? –  kaiser Aug 27 '11 at 20:23
1  
what the heck is that? –  Drai Aug 27 '11 at 23:45
1  
That means that you didn't accept answers to your Qs as such (and therefore mark them as solution - the check mark right below the votes). Point is that you really need to comment on answers if they are not your solution. And if they are what you were searching after, then you should mark it. See "Housekeeping Note" by Chip Bennet below. Please go back and mark your solutions. –  kaiser Aug 28 '11 at 11:10
1  
always good to read the rules! Thanks I get it now. –  Drai Oct 11 '11 at 3:09

1 Answer 1

up vote 3 down vote accepted

I'm assuming you're going to be outputting this on a singular page (Post, Page, Attachment)?

If so, the easiest thing to do is to put your post AND sidebar content inside the Loop, so that you have access to the $post global, and post-related template tags.

Alternately, you could define a variable, inside the Loop, to hold $post-ID so that you can reference it outside of the Loop, i.e. in your sidebar.

In either case, inside the Loop, you would have e.g.:

$post_id = get_the_ID();

Then, in your sidebar:

if ( has_post_thumbnail( $post_id ) ) {
    the_post_thumbnail( 'sidebar_post_thumbnail' );
} else {
    // code to output your default image
}

Note: I'm assuming you'll create a custom image size to output in the sidebar, e.g.

add_image_size( 'sidebar_post_thumbnail', 150, 9999, false );

(which goes in functions.php, of course)

share|improve this answer
    
Worked like a charm! Thanks. used on single.php –  Drai Aug 28 '11 at 0:02
1  
Great! Housekeeping note: you have a 0% accept rate. The WPSE modus operandi is to mark answers as accepted if they solve your problem/answer your question. Otherwise, you may find that people will stop answering your questions. –  Chip Bennett Aug 28 '11 at 0:19

Your Answer

 
discard

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.