简体   繁体   中英

How to show post thumbnail in wordpress front page

I have following code written in function.php in my wordpress.

function wptuts_recentpost2($atts, $content=null){
$getpost = get_posts( array('number' => 1, 'offset' => 1) );
$getpost = $getpost[0];
$return = $getpost->post_thumbnail . "<br />" . $getpost->post_title . "<br />" . $getpost->post_excerpt . "…";
$return .= "<br /><a href='" . get_permalink($getpost->ID) . "'><em>read more →</em></a>";
return $return;
}
add_shortcode('newestpost2', 'wptuts_recentpost2');

What should I write here to display thumbnails?

I tried adding

$getpost = $get_post->post_thumbnail

But it didn't work... :(

Thank you for your time!

You could use get_the_post_thumbnail($getpost->ID) .

Please refter to the codex https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

This will return the thumbnail image tag.

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