Sydney
Sydney

Reputation: 1429

How to use get_post_meta to get thumbnail in wordpress

I have a wordpress website and I am trying to get the thumbnail image (aka featured image) of a post. I can't use the below function because I am not in a loop for reason relating to layout.

the_post_thumbnail_url( ) 

I am just wondering whether I can get the thumbnail of the post via this function.

$thumb_1 = get_post_meta( '40', 'thumb', true );      //40 is the post_id

I can pretty much retreive all other custom fields but, retrieving thumb seems to not work. Does anyone knows what is the "key" for the thumbnail image?

Upvotes: 1

Views: 3753

Answers (1)

Greg Burkett
Greg Burkett

Reputation: 1945

Perhaps a better way to do this would be to use the function get_the_post_thumbnail_url(), which lets you pass in a post ID (and doesn't need to be in the loop:

https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

so $thumb = get_the_post_thumbnail_url(40);

Upvotes: 4

Related Questions