php - 为什么 get_post_thumbnail 函数不输出缩略图的 url?

标签 php jquery html css wordpress

我正在制作一个新的 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(&quot;&quot;); 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/

相关文章:

php - 为什么未设置 magento 前端 cookie

javascript - 如何为 YouTube 视频添加启动画面/占位符图像

php - 生成唯一下载链接仅下载一次

php - 我们如何将 php 页面属性值发送到 php 类方法

php - MYSQL中如何使用触发器在指定日期到达时对表进行更改?

javascript - 如何在背景 URL 的特定部分创建悬停光泽效果

当 &lt;!doctype html> 以正确的格式插入时,Javascript 不工作

javascript - 在 JavaScript 中使用函数创建对象

javascript - 更改 iframe 的 src 不起作用

javascript - 自定义GSAP "hacked"鼠标滚轮滚动