| WordPress
Featured Image Support to a WordPress Theme

WordPress themes makes it possible to give your users control of many aspects of their site's designs.

One of the most common design features in themes are featured images. Many bloggers love to use large, high-quality images for their posts.

In this tutorial, I'm going to show you how to add featured image support in a custom WordPress theme.

Step #1. Add featured image setting in the dashboard

Edit your theme's functions.php file and add the code below:

function mytheme_post_thumbnails() {
    add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_post_thumbnails' );

When you create or edit a post, the new setting will be displayed:

Featured Image Support to a WordPress Theme

Step #2. Display the featured image in single posts

Look for the theme file that renders individual posts on your site. Often this file is called single.php.

Inside the while() loop add this code:

<?php 
if( has_post_thumbnail() ):
    echo get_the_post_thumbnail();
endif; 
?>

The code above checks whether an image exists for the post/page and then prints it with get_the_post_thumbnail() function.

After your new code is integrated into the loop, it would look similar to this:

<?php
while ( have_posts() ) : the_post();

    if( has_post_thumbnail() ):         echo get_the_post_thumbnail();     endif;  

endwhile; ?>

Step #3. Check the end result

  • Create or edit a post.
  • Add a featured image.
  • Preview the changes:
Featured Image Support to a WordPress Theme

Summary

Hopefully this helps you. A stylish Featured Image really helps improve any WordPress post.

Click here you need to make sure every post has a featured image.

Any questions, please feel free to ask in the comments ...


About the author

Valentín creates beautiful designs from amongst the tequila plants of Jalisco, Mexico. You can see Valentín's design work all over this site and you can often find him helping members in support.