【发布时间】:2020-07-06 03:44:20
【问题描述】:
我对 WP 很陌生,所以这似乎是一个非常简单的问题。我正在尝试通过 css 类和高级自定义字段提供背景图片 url。我想这样做,这样我就可以保持课堂清洁干燥。该图像作为自定义字段图像存储在 WP admin 中。这是我的英雄模板代码:
<?php
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php
$hero_img = get_field('hero_image');
?>
<h3>ACF Field Data: <?php the_field('hero_image');?></h3> // just to see what it is
<section class="panel--hero panel--bg-image" <?php $hero_img ? print 'style="--background-image: url("hero_img[5]");"' : ''; ?>>
<div class="panel__content">
<div class="container container__grid page-heading">
<div class="container__col">
<h2 class="page-title center--flex text-white"><?php the_title() ?></h2>
</div>
</div>
<div>
</section>
<?php
endwhile;
endif;
?>
还有scss:
&--bg-image {
position: relative;
height: 50vh;
&::after {
background-image: var(--background-image);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
bottom: 0;
content: '';
display: block;
opacity: var(--opacity);
left: 0;
right: 0;
position: absolute;
top: 0;
z-index: -1;
}
}
没有图像显示。绝对路径是该数组中的第五项,它没有通过。在 JS 中,我通常会循环遍历它,但我认为我已经使用变量声明上方的“if/while”循环完成了该操作。我现在还需要循环 $hero_img 变量吗?如果是这样,那是在 WP/PHP 中执行此操作的最常规方法吗? forEach 是一个选项吗?也许我不清楚仅通过运行 if/while 循环就可以获得数据。任何帮助将非常感激!!谢谢。
【问题讨论】:
标签: php wordpress background-image advanced-custom-fields