Get a Post’s Featured Image the WordPress Way (in the Loop)

The snippet below will get an image tag with src, class, alt, and title attributes. This is a very simple and SEO friendly way of adding images since the alt and title attributes are filled in automatically.

You can use this anywhere in the WordPress loop (archive page or single post page).

get_post_thumbnail_id( get_the_ID()) gets the ID of the post’s featured image which is the only required parameter of wp_get_attachment_image().

<?php echo wp_get_attachment_image( get_post_thumbnail_id( get_the_ID()) ); ?>

This is a great alternative to using the following snippet which only creates an image tag without any alt, class, or title attributes.

<img src="<?php echo get_the_post_thumbnail_url(); ?>" /> //different way of adding images