advanced-custom-fields - ACF Gallery - 使用 Bootstrap 4 一次显示 4 个项目
问题描述
我在 WordPress ACF 中使用图库字段,我想一次只显示四个图库。因此,为了实现这一点,我在 Bootstrap 4 中获取了一些用于多轮播的代码,并希望使用 ACF Gallery 和 PHP 来实现同样的效果。
这是我的代码尝试:
<div class="row blog">
<div class="col-md-12">
<div id="blogCarousel" class="carousel slide" data-ride="carousel">
[...]
<!-- Carousel items -->
<div class="carousel-inner">
<?php
$images = get_sub_field('gallery');
$i = 0;
$j = 0;
$internal_counter = 0;
?>
<?php while ( $i < $gallery_size ): ?>
<?php if ( $i % 4 === 0 ): ?>
<div class="carousel-item <?php if ( $internal_counter === 0 ): echo 'active'; endif; ?>">
<div class="row">
// BEGIN this is the part where i'm stuck on
<?php for ( $j = 0; $j < count ( $images ); $j++ ): ?>
<div class="col-md-3">
<a href="<?php echo $images[$j]['url']; ?>" data-toggle="lightbox" data-gallery="dayhome-gallery">
<img src="<?php echo $images[$j]['sizes']['large']; ?>" alt="<?php echo $images[$j]['alt']; ?>" />
</a>
</div>
<?php endfor; ?>
// END this is the part where i'm stuck on
</div>
</div>
<?php $internal_counter++; ?>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
</div>
</div>
</div>
</div>
我在上面的评论中强调了我坚持的地方。现在它只为每个轮播项目一次显示 8 张图片,如下图所示:
这是我想要的效果:
因此,在这个特定的画廊中,我想一次只展示四张图片。我正在考虑执行这样的操作:
<?php
if ( $j % 4 === 0 ):
break;
endif;
?>
...但它立即中断,因为我开始$j
为 0,因为我还需要画廊中的第零个项目。
感谢您对我的代码的任何帮助。
解决方案
解决了!!!
<div class="carousel-item <?php if ( $internal_counter === 0 ): echo 'active'; endif; ?>">
<div class="row">
<?php while ( $j < count( $images ) ): ?>
<div class="col-md-3">
<a href="<?php echo $images[$j]['url']; ?>" data-toggle="lightbox" data-gallery="dayhome-gallery">
<img src="<?php echo $images[$j]['sizes']['large']; ?>" alt="<?php echo $images[$j]['alt']; ?>" />
</a>
</div>
<?php if ( ++$j % 4 === 0 ): ?>
<?php break; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</div>
在将变量与模数 4 进行比较之前,我必须确保++$j
对变量进行预迭代。
推荐阅读
- amazon-web-services - terraform 在另一个资源中使用来自 for_each 的特定值
- jenkins - archiveArtifacts 步骤无法与 Jenkins 通信
- python - 带有条件的数据框中的样本
- spring-boot - 如何使用邮递员测试下面给出的获取请求?
- flutter - 如何在颤动中将 ListTile 连接到音频小部件
- scala - 在 Spark for Scala 中导入 UDF(用户定义函数)的问题
- javascript - Javascript 给我 Uncaught ReferenceError: require is not defined 错误
- linux - Shell命令中引号中的冒号
- javascript - 如何将 JSON 保存到 QT 中的本地文本文件?
- libgit2 - 来自 libgit2 的“git_proxy_options 上的无效版本 0”:原因?使固定?随便走走?