背景-建立了一个WordPress网站,并利用ACF高级自定义字段在我的页面中生成输入字段。
然后我填充了页面字段,以便可以生成/使用此变量:
<?php
$case_study_image_gallery = get_field('case_study_image_gallery');
?>
然后我在while循环中调用子字段,然后在使用该变量拆分图像数组时调用['url']。
<?php if( $case_study_image_gallery ) : ?>
<div class="case-study-slider">
<?php
while (have_rows('case_study_image_gallery')): the_row();
$case_study_image = get_sub_field('case_study_image');
?>
<div class="case-study-slide">
<img class="case-study-slider-image" src="<?php echo $case_study_image['url']; ?>">
</div>
<?php endwhile; ?>
</div>
<div class="case-study-slider-nav">
<?php
while (have_rows('case_study_image_gallery')): the_row();
$case_study_image = get_sub_field('case_study_image');
?>
<div class="case-study-nav-slide">
<img class="case-study-slider-nav-image" src="<?php echo $case_study_image['url']; ?>">
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
如您所见,由于将滑块图像和较小的缩略图图像都需要使用相同的值,因此我将我的时间分为多个部分。为此,我必须将此代码放在子字段图像的两种用法之间。
<?php endwhile; ?>
</div>
<div class="case-study-slider-nav">
<?php
while (have_rows('case_study_image_gallery')): the_row();
$case_study_image = get_sub_field('case_study_image');
?>
这显然不起作用,因为第一个循环烧毁了图像实例。
我实质上是在寻找一种方法,可以在不重新启动/循环遍历并触发下一行数据的情况下从循环中中断。像这样:
<?php if( $case_study_image_gallery ) : ?>
<div class="case-study-slider">
<?php
while (have_rows('case_study_image_gallery')): the_row();
$case_study_image = get_sub_field('case_study_image');
?>
<div class="case-study-slide">
<img class="case-study-slider-image" src="<?php echo $case_study_image['url']; ?>">
</div>
<?php
HAVE A BREAK FROM THE WHILE;
?>
</div>
<div class="case-study-slider-nav">
<?php
CONTINUE WHILE
?>
<div class="case-study-nav-slide">
<img class="case-study-slider-nav-image" src="<?php echo $case_study_image['url']; ?>">
</div>
<?php endwhile; ?>
</div>
我很高兴有一种方法可以在循环完成后重置循环,但是我觉得应该有一种方法可以利用我在这里尝试的代码。
响应Simonw16:
<div class="cs-slider">
<div class="case-study-slider">
<div class="case-study-slide"><img class="case-study-slider-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/silicone-elastomer-tube.png"></div>
<div class="case-study-slide"><img class="case-study-slider-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/silicon-sputtering-target.jpeg"></div>
<div class="case-study-slide"><img class="case-study-slider-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/zirconium-lump.jpg"></div>
</div>
<div class="case-study-slider-nav">
<div class="case-study-nav-slide"><img class="case-study-slider-nav-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/silicone-elastomer-tube.png"></div>
<div class="case-study-nav-slide"><img class="case-study-slider-nav-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/silicon-sputtering-target.jpeg"></div>
<div class="case-study-nav-slide"><img class="case-study-slider-nav-image" src="https://localhost/materialshub/wp-content/uploads/2018/09/zirconium-lump.jpg"></div>
</div>
</div>
答案 0 :(得分:2)
您可以这样做吗?
```
<?php
$image_urls = [];
while (have_rows('case_study_image_gallery')):
the_row();
$image_urls[] = get_sub_field('case_study_image');
endwhile;
?>
<div class="case-study-slider">
<?php foreach($image_urls as $image_url): ?>
<div class="case-study-slide">
<img class="case-study-slider-image" src="<?php echo $image_url; ?>">
</div>
<?php endforeach; ?>
</div>
<div class="case-study-slider-nav">
<?php foreach($image_urls as $image_url): ?>
<div class="case-study-nav-slide">
<img class="case-study-slider-nav-image" src="<?php echo $image_url; ?>">
</div>
<?php endforeach; ?>
</div>
```