2015-02-07 55 views
0

我所有的發佈縮略圖,我目前正在使用一個佔位符圖像工作正常,但希望使用特色圖像形式的帖子作爲背景,這裏是我的代碼...WordPress的發佈縮略圖問題

<?php 

$args = array('posts_per_page' => 10, 'category' => 3); 

$myposts = get_posts($args); 
foreach ($myposts as $post) : setup_postdata($post); ?> 

     <div class="box" style="background-image:url('<?php the_post_thumbnail(); ?>');"> 
      <div class="overlay"> 
       <div class="buttonContainer"> 
        <a href="<?php the_permalink(); ?>"> 
         <button>preview</button> 
        </a> 
       </div> 
      </div> 
     </div> 


<?php endforeach; 
wp_reset_postdata();?> 

當我在瀏覽器中查看它時,它會打印出此... ');">而且沒有圖像...我在哪裏出錯了?

+0

你試過'echo the_post_thumbnail()'嗎? – pbaldauf 2015-02-07 20:09:42

+0

我做了,沒有出現... – coder123 2015-02-07 20:09:57

+0

嘗試移出它'<?php the_post_thumbnail(); ?'''''style'標籤,並將其與''標籤一起放置,以便您可以直接看到它的內容。 – Stickers 2015-02-07 20:33:06

回答

1

the_post_thumbnail()返回一個img元素,如;

<img src="some-image-url' /> 

這不是你想要傳遞給背景圖像的東西。

你需要的東西,如:

$attachment_id= get_post_thumbnail_id(the_ID()); 
$img_data = wp_get_attachment_image_src($attachment_id, the size you want); 

那麼你可以回聲$ img_data [0]得到附件的URL。

+0

我可以從上面的代碼中詢問如何將這個作爲背景圖片來介紹嗎? – coder123 2015-02-07 20:49:12

+1

用echo $ img_data [0]替換the_post_thumbnail()。 – bobdye 2015-02-07 21:07:14