简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM