How to display a thumbnail of a post with a background image

How to display a thumbnail of a post with a background image

Wordpress

To display a thumbnail of a post as a background image, it is sufficient to use the following code:

<?php
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_object = get_post($thumbnail_id); 
echo ($thumbnail_object->guid); ?>

This code gets the URL of the attached image. On an example it looks like this:

<div class="background" style="background: url(' <?php
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_object = get_post($thumbnail_id); 
echo ($thumbnail_object->guid); ?> ')">

The code is basically suitable for entries, pages, products and product categories.