[英]How to get the featured image and put it's image path in to inline styles ie. <div style=“background:url(filepath/here)”;>?
I've got this line of code - 我有这行代码 -
<?php the_post_thumbnail('latest-img'); ?>
which is pulling in the correct featured image, however when I try and put - 这是在拉动正确的特色图像,但是当我尝试并放 -
<div class="blog-image" style="background:url('<?php the_post_thumbnail('latest-img'); ?>');"></div>
it doesn't show. 它没有显示。
Does anyone know what I'm doing wrong? 有谁知道我做错了什么?
You need to get ID of the post, then get thumbnail ID of the post and use it to get URL of the thumbnail: 您需要获取帖子的ID,然后获取帖子的缩略图ID并使用它来获取缩略图的URL:
<?php
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'latest-img' );
?>
then include URL of image in style of div: 然后在div的样式中包含图像的URL:
<div class="blog-image" style="background:url('<?php echo $image_url[0]; ?>');"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.