我正在制作一个新的 WordPress 主题,同时使用标题 slider (我使用了 BXslider),我坚持使用内联 css 东西 我想使用这个代码 http://jsfiddle.net/0nj2ry4n/1/
但是,我无法使用 the_post_thumbnail($size) 内联 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(""); float: none; list-style: outside none none; position: absolute; width: 1583px; z-index: 50; display: block;" ');'="">
最佳答案
您使用了错误的函数。 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();
关于php - 为什么 get_post_thumbnail 函数不输出缩略图的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43378913/