2017-05-04 61 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