How to get the featured image and put it’s image path in to inline styles ie. <div style=”background:url(filepath/here)”;>?

I’ve got this line of code –

<?php the_post_thumbnail('latest-img'); ?>

which is pulling in the correct featured image, however when I try and put –

Read More
<div class="blog-image" style="background:url('<?php the_post_thumbnail('latest-img'); ?>');"></div>

it doesn’t show.

Does anyone know what I’m doing wrong?

Related posts

1 comment

  1. You need to get ID of the post, then get thumbnail ID of the post and use it to get URL of the thumbnail:

    <?php
    $image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'latest-img' );
    ?>
    

    then include URL of image in style of div:

    <div class="blog-image" style="background:url('<?php echo $image_url[0]; ?>');"></div>
    

Comments are closed.