WordPress ‘Featured Image’, how to use…

WordPress stands for most common and popular CMS now a days. Sometimes we need just a media field for a image to be added in a post. WordPress latest 3+ version has this facility, so you don’t need to use any ‘custom field type’.

In default theme, this feature enabled by default, but when you are using a custom made theme or a theme of your choice which you downloaded, but not supported ‘featured Image’ section, then what to do.

Its very simple for wp developer to add that support to your theme. Add below code to your theme’s functions.php file :

add_theme_support('post-thumbnails');

Now you will see that section in your admin sidebar when adding/editing any page/post. And also you will find option at your ‘Screen Options’ section as below, so from there you can enable/disable that for future need.

In right sidebar, ‘Featured Image’ section after adding a image:

Now, You knew how to add theme support and how to add an image on admin panel. So to show this image on front-end, in any single post/page or in any loop, just add below code to show it between wp post loop:

<?php the_post_thumbnail();?>

This will show that image as you added in admin panel with that image ratio. Note that this code will execute when you are using wordpress’s default loop code like <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> and <?php endwhile;  ?>

If you use custom loop code using other function like get_posts() or custom query like $wpdb->get_results(), then it depends on that way and you have to pull data with correspondence format.

Use, modify and share your thoughts and happy blogging 🙂

Advertisements