How to add and get post thumbnails or featured images in WordPress

Featured images or post thumbnails are a common and popular feature of WordPress themes. Today most of WordPress themes like photography, news, blog, restaurant themes,and other types of themes have built-in support for post thumbnails. In this article we will show you how to add featured images or post thumbnails in WordPress.

Add Featured Image or Post Thumbnails in WordPress

Featured image is a popular and common feature supported by a most of themes, but might be your theme does not support featured images functionality. In that case, you can add featured image support to your theme. If you are have some coding or editing knowledge of WordPress theme files, then you can do it yourself.
To add featured image or post thumbnails support in a WordPress theme, you have need to add this line of code in your theme’s functions.php file:

    add_theme_support( 'post-thumbnails' ); 

This line of code will enable featured image or post thumbnails support in your WordPress theme for all posts and pages. You can now open the posts or pages, and you will see featured image option enabled.

Set Custom Size of Featured Image or Post Thumbnails in WordPress

You can also set additional image sizes to use with the_post_thumbnail() function. To set image size for featured images you upload, you need to add this line of code to your functions.php file.

    set_post_thumbnail_size( 100, 100); 

Set Custom Size And Name of Featured Image or Post Thumbnails in WordPress

You can also set additional image sizes and name to use with the_post_thumbnail() function. To set image size and name for featured images you upload, you need to add this line of code to your functions.php file.

    add_image_size( 'large-thumbnail', 600, 300 ); // "large-thumbnail" name of thumbnail
    // this type of thumbnails not access directly, also have need to write name of thumbnail.
    <!?php the_post_thumbnail( 'single-post-thumbnail' ); ?>

Get or Show Featured Image or Post Thumbnails in WordPress

When you set a featured image it will not show automatically in your WordPress theme. To display featured images in your theme, you need to edit your templates and add this line of code where you want to display the featured image:

    <!?php the_post_thumbnail(); ?> 

OR

    $thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID ), 'thumbnail');
    <img src="<?php echo $thumb_image_url[0]; ?>" alt="<?php echo the_title();?>" title="<?php the_title();?>" />

Set if condition for Show Featured Image or Post Thumbnails in WordPress

You can also set if condition for show Post Thumbnails in WordPress if post thumbnail is set or added then thumbnail will be display otherwise your by default no image found image will be display.

     <?php
     $thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID ), 'thumbnail');
     if(has_post_thumbnail()){?>
     <img src="<?php echo $thumb_image_url[0]; ?>" alt="<?php echo the_title();?>" title="<?php the_title();?>" />
     <?php }else{?>
     <img src="<?php echo get_template_directory_uri(); ?>/images/395x396.jpg" title="<?php the_title();?>" alt="<?php echo the_title();?>" />
     <?php }?>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.