2

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

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

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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