How to add post featured image url to inline background url()?

I’d like to get a post’s featured image and use it in the style=”background: url()” of the post. I thought this would do it, but alas I was wrong because this outputs the whole (img src=””> stuff.

<div style="background: url(<?php the_post_thumbnail( 'thumbnail' ); ?>
) !important;">adasdasdasd </div>

I tried get_post_thumbnail but don’t think I have the format right.

Read More

Any thoughts?

Related posts

Leave a Reply

1 comment

  1. You just need to retrieve the src attribute of the thumbnail:

    $thumb_src = wp_get_attachment_image_src ( get_post_thumbnail_id('thumbnail'));
    

    $thumb_src is then an array containg the url, width and height of the thumbnail image. So something like…

    <div style="background: url(<?php echo $thumb_src[0];?>
    ) !important;">adasdasdasd </div>
    

    should work.