2017-04-12 73 views
1

我想提出一個新的WordPress主題,頭部滑塊(我用BXslider)我堅持內聯CSS的事情 我想使用此代碼 http://jsfiddle.net/0nj2ry4n/1/爲什麼get_post_thumbnail函數沒有輸出縮略圖的url?

但是,我無法使用the_post_thumbnail($大小)在線工作時CSS背景

<li style='background-image: url(the_post_thumbnail($size));'> 

我的代碼是

while($slider->have_posts()): $slider->the_post(); 
    $myimg = get_post_thumbnail('myslider'); 

    echo "<li class='featured-post' style='background-image: url('$myimg');'>"; 
    echo "</li>"; 
endwhile; wp_reset_postdata(); 
echo "</ul>"; 

*更新*

現在,如果我試過$ myimg = the_post_thumbnail_url('myslider');得到這種類型的HTML,但我仍然沒有弄清楚如何解決這個問題。

<ul class="slider slides" style="width: auto; position: relative;"> 
http://localhost/demo/myweb/wp-content/uploads/2017/03/AAC-CAREERS2-1349x499.jpg 
<li class="featured-post" style="background-image: url(&quot;&quot;); float: none; list-style: outside none none; position: absolute; width: 1583px; z-index: 50; display: block;" ');'=""> 
+0

這是一個WordPress的問題。你應該使用WordPress的堆棧。 – Cam

+0

@Cam這裏已經有111,000個問題用'Wordpress'標記了。還有一個不會傷害。 – vlasits

+0

我確信有11.1萬人不知道有一個WordPress的堆棧...知識就是力量我的朋友。 – Cam

回答

1

您正在使用錯誤的功能。 get_post_thumbnail('myslider')返回html,而不是圖片url字符串。

你想the_post_thumbnail_url($size);它回聲的URL,所以你將無法將它存儲在一個變量。像這樣內聯使用它。

while($slider->have_posts()): $slider->the_post(); 
    echo '<li class="featured-post" style="background-image: url(' . the_post_thumbnail_url('myslider'); . '">'; 
    echo "</li>"; 
endwhile; 
wp_reset_postdata(); 
+0

我試過你說的話,用我得到的html更新我的問題,你能解決這個問題嗎? – faysal

+0

啊...對不起。我忘了the_post_thumbnail_url()迴應了url而不是返回一個字符串。 – vlasits