繁体   English   中英

如何获取特色图像并将其图像路径放入内联样式,即。 <div style=“background:url(filepath/here)”;> ?

[英]How to get the featured image and put it's image path in to inline styles ie. <div style=“background:url(filepath/here)”;>?

我有这行代码 -

<?php the_post_thumbnail('latest-img'); ?>

这是在拉动正确的特色图像,但是当我尝试并放 -

<div class="blog-image" style="background:url('<?php the_post_thumbnail('latest-img'); ?>');"></div>

它没有显示。

有谁知道我做错了什么?

您需要获取帖子的ID,然后获取帖子的缩略图ID并使用它来获取缩略图的URL:

<?php
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'latest-img' );
?>

然后在div的样式中包含图像的URL:

<div class="blog-image" style="background:url('<?php echo $image_url[0]; ?>');"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM