Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取特色图像并将其放置';将图像路径设置为内联样式,即<;div style=";背景:url(文件路径/此处)“>;?_Javascript_Html_Wordpress_Wordpress Theming_Background Image - Fatal编程技术网

Javascript 如何获取特色图像并将其放置';将图像路径设置为内联样式,即<;div style=";背景:url(文件路径/此处)“>;?

Javascript 如何获取特色图像并将其放置';将图像路径设置为内联样式,即<;div style=";背景:url(文件路径/此处)“>;?,javascript,html,wordpress,wordpress-theming,background-image,Javascript,Html,Wordpress,Wordpress Theming,Background Image,我有这行代码- <?php the_post_thumbnail('latest-img'); ?> 这是拉在正确的特色形象,但当我试图把- <div class="blog-image" style="background:url('<?php the_post_thumbnail('latest-img'); ?>');"></div> 您需要获取文章的ID,然后获取文章的缩略图ID,并使用它获取缩略图的URL: <?php $i

我有这行代码-

<?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>

如果你写了什么输出?如果你把这个输出放进去,它能工作吗?控制台中有错误吗?这只是给了我包装在div ie-中的特色图像。这就是我已经拥有的,但是我需要它是一个背景图像,它需要是动态的(所以没有硬编码图像路径)。如果是这样的话,那么你需要拿出一些。你不想要
,也不想要
“/>
只有图像链接。是的,这是正确的,但当我尝试从wordpress.stackexchange(或许多其他快速谷歌搜索)时