Become Wordpress Professional - Your Ultimate Wordpress Guide

Wednesday, 27 March 2013

How to Add Featured Image Thumbnail in your WordPress theme


What is Featured Image

WordPress Featured image was firstly introduced in WordPress version 2.9. It is the very unique way of WordPress to represent post and pages with dynamic images. We can just attach post with feature image by uploading image in featured image area box appeared in the right side of write post interface.

How to enable featured image in the wordpress

To enable feature image in wordpress, we have to add the following code in function.php available within the wordpress theme.
add_theme_support( 'post-thumbnails' );

How to set featured image size in wordpress

We can set different sizes for the featured image by adding little more code in function.php however we can also set default sizes of images from the WordPress Admin panel you can find here (Settings > Media)

set_post_thumbnail_size(100, 50); 

NOTE: In above case feature image will be auto re-size it self with equal proportional.(in above case ratio is 2:1) It means if we will upload image with the resolution of 50px x 100px it will auto re-size image to 25px x 50px. We can also use cropping image method by adding another argument "true" in the above code. It will simply crop the extra amount of pixels to make image fit the featured image area.

set_post_thumbnail_size(100, 50, true); 

How to display Featured Image in WordPress Theme or WordPress Template

You can display feature image anywhere in the WordPress theme by adding the following code in your theme's template file.
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?> 

You might also like: How to Create Widget areas in Wordpress Themes

2 comments:

  1. It's funny how I was just browsing through this website and found this simply but really useful tutorial. I wanted to integrate a thumbnails into my wordpress theme but had no clue how I should do it. So I will bookmark this one for later. Thanks Farsab

    ReplyDelete

Please Avoid Spamming. Comments will be moderated before they are published.