简体   繁体   English

背景图像未显示

[英]Background-image is not being displayed

I'm pulling a featured image in from a blog post and setting to my background image.我正在从博客文章中提取特色图片并设置为我的背景图片。 I'm using the same line of code I usually do, but for some reason no image is being displayed.我使用的是我通常使用的同一行代码,但由于某种原因没有显示图像。

$img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');

<div class="panel--bg-image" <?php $img ? print 'style="--background-image: url(' . $img['sizes']['large'][0] . ');"' : ''; ?>>

To make sure my variable isn't empty, I ran为了确保我的变量不为空,我跑了

var_dump($img)

and my image is indeed being called.我的形象确实被调用了。 When I check the element in the dev tools, I see this:当我检查开发工具中的元素时,我看到: 在此处输入图片说明

When an image is successfully being called, I can always see the file in the url().成功调用图像后,我始终可以在 url() 中看到该文件。 I usually pull these images from a custom filed, but these are being pulled from 'featured image' in the post backend.我通常从自定义文件中提取这些图像,但这些图像是从帖子后端的“特色图像”中提取的。 Any idea what I'm doing wrong here?知道我在这里做错了什么吗?

The WordPress function wp_get_attachment_image_src requires the Image-ID and the size and returns an array: WordPress 函数wp_get_attachment_image_src需要 Image-ID 和大小并返回一个数组:

array {
    [0] => url,
    [1] => width
    [2] => height
    [4] => is_intermediate
}

So, if you want to get the Image-URL, use $img[0] instead of $img['sizes']['large'][0] .因此,如果您想获取图像 URL,请使用$img[0]而不是$img['sizes']['large'][0]

Source: WordPress Codex来源: WordPress Codex

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

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