faysal
faysal

Reputation: 31

Why doesn't get_post_thumbnail function output the url of the thumbnail?

I am making a new wordpress theme while working with header slider (i used BXslider) i stuck on inline css thing I wanna use this code http://jsfiddle.net/0nj2ry4n/1/

However, I am unable to use the_post_thumbnail($size) inline css background

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

my code is

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>";

* Updates *

now if i tried $myimg=the_post_thumbnail_url('myslider'); got this type of html and still i didnt figure it out how to solve this.

<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;" ');'="">

Upvotes: 2

Views: 252

Answers (1)

vlasits
vlasits

Reputation: 2235

You're using the wrong function. get_post_thumbnail('myslider') returns html, not an image url string.

You want the_post_thumbnail_url( $size ); It echos out the url, so you won't be able to store it in a variable. Use it inline like so.

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();

Upvotes: 2

Related Questions