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.

I need to duplicate a list of thumbnails elsewhere on a page and have set up a few different custom thumbnail sizes in my functions.php file.

If I use:

the_post_thumbnail('photo-small');

Then the thumbnail gets displayed correctly, if I use:

echo get_post_thumbnail('photo-small');

Nothing gets echoed - it's an empty string.

What I am trying to do is add the generated img tags to an array so that I can loop through it outside of the loop but for some reason it's always blank.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You're using get_the_post_thumbnail() and not the nonexistent get_post_thumbnail() function, right?

As JohnG said, you have to pass the ID of the current post to get_the_post_thumbnail() (the the_post_thumbnail() function already handles that for you). The Function Reference in the WordPress Codex has many usage examples:

get_the_post_thumbnail($id);                  // without parameter -> Thumbnail

get_the_post_thumbnail($id, 'thumbnail');     // Thumbnail
get_the_post_thumbnail($id, 'medium');        // Medium resolution
get_the_post_thumbnail($id, 'large');         // Large resolution

get_the_post_thumbnail($id, array(100,100) ); // Other resolutions

Where $id is the ID of the current post. You can get it via get_the_ID().

share|improve this answer
    
Yep, that was it. What threw me was that the codex says the passing the id is optional at the top where it lists parameters; I misunderstood JohnG's response - it sounded like I only needed to include the ID if it was outside of the loop. Thanks for clearing that up –  ollie Mar 16 '12 at 11:54

dumb question, but are you calling the_post_thumbnail from within a loop? If not, then you would want to use get_post_thumbnail(), but you'd need to pass in the post_id you want the featured image for

share|improve this answer
    
Yeah I am, I have it set up in the loops so that the_post_thumbnail('photo-small') is exectued which displays the image, but then in the same loop I add $images[] = get_the_post_thumbnail('photo-large'). the_post_thumbnail('photo-large') successfully echoes it. Makes no sense to me –  ollie Mar 16 '12 at 2:03

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.