1

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.

Any thoughts?

1 Answer 1

4

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.