简体   繁体   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)”;>?

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.

相关问题 如何编写JQuery函数以从内联样式的div中获取背景图像并将其放在图像标签中? - HOw can I write a JQuery function to take background-image from an inline-style div and put it in an image tag? 正则表达式以内联样式匹配url() <div style=“url()”> - Regex to match url()'s in inline styles <div style=“url()”> 如何判断Google的Streetview Image API是否返回“抱歉,我们这里没有图像”(即NULL)结果? - How can I tell if Google's Streetview Image API Returns “Sorry, we have no imagery here” (ie. NULL) Result? 如何使用element.style.backgroundImage =“ url(filePath)”将背景图像添加到javascript? - How do I add a background image to javascript using element.style.backgroundImage = “url(filePath)”? 如何获取div背景图片的xpos? - How to get xpos of a div's background image? 如何防止(内嵌样式)背景图片被加载? - How to prevent (inline style) background image from being loaded? 如何在反应中设置背景图像内联样式的高度和宽度 - How to set height and width of background image inline style in react 如何延迟加载内联样式属性(React)中的背景图像? - How to Lazy load the background image inside the inline style property (React)? Javascript通过其背景图片获取div? - Javascript get div by it's background image? Cheerio / NodeJS获取div的背景图片 - Cheerio/NodeJS get a div's background image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM