Impostare l'immagine in evidenza come sfondo
Ho una pagina singola per i miei prodotti dove vengono elencati tutti i prodotti. Ecco come l'ho realizzata:
<ul class="produkte">
<?php $args = array( 'post_type' => 'produkte', 'posts_per_page' => 30 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li class="produkt">';
echo '<a href="' . get_permalink() . '">';
echo '<div class="produkt-info">';
the_title('<h3>', '</h3>');
if (has_post_thumbnail()) {
echo '<figure class="imgBox">';
the_post_thumbnail('full', array('class' => 'img-resp') );
echo "</figure>";
}
echo '</div>';
echo '</a>';
echo '</li>';
endwhile; ?>
</ul>
Ho deciso di voler impostare l'immagine in evidenza come sfondo dell'elemento li.produkt. Quando faccio qualcosa del genere:
echo '<li class="produkt" style="background: url('.the_post_thumbnail().')">';
La pagina genera un elemento immagine sopra l'elemento li. Cosa sto sbagliando??

Ora esiste la funzione get_the_post_thumbnail_url
fornita in WordPress.
Esempio di utilizzo dal codex:
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

Secondo il WordPress Codex, la funzione the_post_thumbnail() Mostra l'immagine in evidenza del post. https://developer.wordpress.org/reference/functions/the_post_thumbnail/ Ma questa restituisce l'intero tag immagine e non solo l'URL src, che nel tuo caso è ciò che ti serve.
Invece dovresti usare la funzione wp_get_attachment_image_src() che Recupera un'immagine per rappresentare un allegato.. https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
Quindi ti suggerisco di provare qualcosa del genere:
//questo recupera la versione completa dell'immagine
$image_data = wp_get_attachment_image_src ( get_the_post_thumbnail( $post->ID), 'full' );
$image_url = $image_data[0];
echo '<li class="produkt" style="background: url('. $image_url .')">';

Prova a utilizzare background-size: cover, background-position: center e background-size: 300px 100px;
<?php
echo '<li class="produkt" style="background-image: url('<?php the_post_thumbnail_url(); ?>'); background-size: cover; background-position: center; background-repeat: no-repeat; background-size: 300px 100px;">';
?>
