2017-05-04 34 views
0

我無法顯示我的背景圖像。在循環內顯示背景圖像和if語句wordpress

我創建了一個循環來顯示我的帖子,然後試圖設置背景圖像,但是它打破了某個地方?以下是我在循環中的代碼。

<div class="artist-feed"> 
<?php 
    $artistloop = new WP_Query(array('post_type' => 'artist')); 
    if ($artistloop->have_posts()) : 
     while ($artistloop->have_posts()) : $artistloop->the_post(); ?> 

       <a href="<?php the_permalink(); ?>"> 
        <div class="single-artist" style="background-image: url(<?php echo the_post_thumbnail(); ?>);"> 
        <div class="artist-info"> 
         <h2><?php echo get_the_title(); ?></h2> 
        </div> 
        </div> 
       </a> 
     <?php endwhile; 
     endif; 
    wp_reset_postdata(); 
?> 
</div> 

回答

0

你的問題是與調用the_post_thumbnail()。這種方法實際上顯示縮略圖,而不是它的URL。在這裏看到:

https://developer.wordpress.org/reference/functions/the_post_thumbnail/

要獲得URL到您的縮略圖,嘗試get_the_post_thumbnail_url()。這應該是你需要的。看到這裏:

https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

+0

謝謝克里斯!這麼簡單......我不敢相信我忽略了它。 –

+0

沒問題:)它花了我永遠也弄清楚了這一點..我只是很高興WordPress的最後添加了一個方法'get_the_post_thumbnail_url()' – Chris