How to get WordPress Post Featured Image URL

Gets the direct image URL for the featured image of the current post. This tag must be used within The Loop. Use has_post_thumbnail() to check whether a Feature Image has been added to the post.

Check the code below and let me know if it works for you.

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">

</div>
<?php endif; ?>

And this one is way to easy to add

<?php 
 wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()))
 ?>

Written By:

Sarmad Gardezi A blog scientist by Mind and a Passionate Blogger by heart. Sarmad is Freelancer, Entrepreneur and Google Apps Scripts Developer from Islamabad, Pakistan.

Subscribe to Email Newsletter