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

Premium WordPress Themes

Reliable Web Hosting

Smart Web Worker strongly recommends HostGator for your website/blog hosting. Sign up for web hosting at just $3.96/month. Use the coupon code "SMARTBLOGGER25" to get 25% discount on any hosting packages. Get an account with HostGator now!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>