get_the_post_thumbnail('thumbnail-name') always returns empty string

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.

Topic thumbnails post-thumbnails Wordpress

Category Web


You can pass null for id as long as it is in a loop.

get_the_post_thumbnail( null, 'large' );

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().


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.