WordPress: Show thumbnail of a post

WordPress: Show thumbnail of a post

If you want to show the featured image or thumbnail of a WordPress post, just use the following code inside the WordPress query loop. Thumbnails can make your website look good.

if( has_post_thumbnail($post->ID) ) {
echo get_the_post_thumbnail( $post->ID, 'thumbnail', array('class' => 'thumbnail') );
}

You can use the above code to show images in ‘thumbnail’, ‘medium’ and ‘large’ sizes as defined in your WordPress media settings. Make sure that the functions.php file of your theme has thumbnail support activated. If not, use the code below to activate thumbnails by placing it in functions.php. You can also add your custom thumbnails using add_image_size.

add_theme_support( 'post-thumbnails' );
add_image_size('themeshot', 300, 225, true);

If you want to call a custom image size ‘themeshot’, replace the word thumbnail with your image size name.

if( has_post_thumbnail($post->ID) ) {
echo get_the_post_thumbnail( $post->ID, 'themeshot', array('class' => 'themeshot') );
}