WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
    
That worked perfectly. Thanks very much Stephen! – Christopher Oct 13 '11 at 19:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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