Add featured image or thumbnail to WordPress post

Featured image or post thumbnail is pretty useful for your WordPress site. Using this feature, you’ll be able to add the thumbnail to WordPress post. This article will explain about how to add featured image or post thumbnail in WordPress.


All modern WordPress themes have already been embedded post thumbnail support. If you are a WordPress theme developer or want to build a custom theme, this tutorial will help you a lot for adding featured image support to your theme.

Enable Featured Image and Set Thumbnail Size

add_theme_support() function is used for enabling the featured image in WordPress. Using set_post_thumbnail_size() function, you can add the default featured image dimensions.
Open the wp-content/themes/themeName/functions.php file and add the following code.

 * Enable support for Post Thumbnails on posts and pages.
add_theme_support'post-thumbnails' );

//Set thumbnail dimensions
set_post_thumbnail_size346230true );

$width => Optional. An integer value of post thumbnail width in pixels.
$height => Optional. An integer value of post thumbnail height in pixels.
$crop => Optional. Boolean value for cropping of post thumbnail. False – Soft proportional crop mode ; True – Hard crop mode.

Additional Image Sizes for Featured Image

By default WordPress has four image sizes, “thumbnail” (150px x 150px), “medium” (300px x 300px), “large” (640px x 640px) and “full” (original size). You can add custom thumbnail size using add_image_size() function.

//custom thumbnail size
add_image_size'featured'200125true );

Upload Featured Image

Go to the Posts » Add New and you’ll see the Featured Image section under the Tags section at the right sidebar.


Click on Set featured image => Upload image and click on Set featured image.


Display Post Thumbnail

the_post_thumbnail() function display the featured image for the current post. If you want to display the featured image in posts listing page, the_post_thumbnail() function must be used in loop.

if ( has_post_thumbnail() ) { 
// Post thumbnail.

Displaying the custom-size thumbnail – In the first parameter of the_post_thumbnail() function, provide the custom size keyword defined by add_image_size().

if ( has_post_thumbnail() ) { 
// Post thumbnail.

You can add the array of attribute/value pairs in the second parameter of the_post_thumbnail() function.

if ( has_post_thumbnail() ) { 
the_post_thumbnail('featured',array('class' => "attachment-$size img-responsive"'alt' => trimstrip_tagsget_post_meta(get_post_thumbnail_id(get_the_ID()), '_wp_attachment_image_alt'true) ) )));

Now you’ll see the featured images in posts listing or where the_post_thumbnail() is included.


Related Articles

Comments 0