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.
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.
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);
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' ); ?>
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();?>" />
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 }?>
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.