Last Updated: February 25, 2016
·
1.623K
· primus202

Fetch Featured Wordpress Post Image

You can use the following block in your template file's main post loop or any post loop. Likewise, you can replace $post->ID with a specific post ID to retrieve a particular posts featured image

<?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; ?>

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

WordPress sites collect scripts from themes, plugins, and tag managers. A compromised plugin update can add a skimmer to every page without a change in your own templates.

Security insights from Cside