Impostare l'immagine in evidenza come sfondo

25 mar 2016, 13:43:19
Visualizzazioni: 38.6K
Voti: 5

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??

0
Tutte le risposte alla domanda 5
0
12

Il problema qui è che the_post_thumbnail() non restituisce l'URL dell'immagine ma il tag img.

Puoi utilizzare questo codice

$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<li class="produkt" style="background: url('. $url.')">';
25 mar 2016 13:54:59
0

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/

15 mag 2018 23:26:13
1

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 .')">';
25 mar 2016 14:08:14
Commenti

Fai solo attenzione che hai una parentesi ")" in più alla fine della tua prima riga.

Simone Cabrino Simone Cabrino
29 mar 2017 17:22:47
0

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;">';
?>
30 nov 2020 17:07:40
0

semplicemente mostra l'URL della miniatura del post:

<li class="produkt" style="background-image: url(<?php echo the_post_thumbnail_url(); ?>);">></li>

10 feb 2021 00:32:55