How do I turn a post thumbnail into a background image?

I believe the answer o this question was posted on this page, but it is still not clear to me. The snippets are scattered all over the discussion and several glitches happen when I implement them.

Related posts

1 comment

  1. I was looking for a code snippet that will extract the URL of a post thumbnail to be used as a background image. Through research elsewhere, the best answer I found is the code snippet below:

    <div style="background: url(<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,'large', true); echo $image_url[0]; ?>)"></div>
    

    This code sets the large thumbnail of a post as the background image in this div tag. From here I believe you should be able to modify this code to your particular situation.

Comments are closed.