WordPress中借助ACF隐藏Repeater内的空字段
隐藏ACF Repeater中的指定空字段
直接在现有Repeater循环里加值判断就能实现需求,不用新增独立Repeater,完全保留原有功能灵活性。
父Repeater处理(隐藏空的pour_sizeright对应的<li>)
在遍历父Repeater的代码中,对pour_sizeright字段做非空判断,只有当字段有内容时才输出对应的<li>元素:
<?php if( have_rows('你的父Repeater字段名') ): ?> <ul> <?php while( have_rows('你的父Repeater字段名') ): the_row(); ?> // 保留其他父字段的输出逻辑 <?php // 仅当pour_sizeright有值时才渲染<li> $pour_sizeright = get_sub_field('pour_sizeright'); if( $pour_sizeright ): ?> <li><?php echo $pour_sizeright; ?></li> <?php endif; ?> <?php endwhile; ?> </ul> <?php endif; ?>
子Repeater处理(隐藏空的wineprice_pourright对应的<dd>)
同样在子Repeater的循环里,对wineprice_pourright做非空判断,有值才输出<dd>:
<?php if( have_rows('你的父Repeater字段名') ): ?> <ul> <?php while( have_rows('你的父Repeater字段名') ): the_row(); ?> // 保留父字段的输出逻辑 <?php if( have_rows('你的子Repeater字段名') ): ?> <dl> <?php while( have_rows('你的子Repeater字段名') ): the_row(); ?> // 保留子Repeater其他字段的输出逻辑 <?php // 仅当wineprice_pourright有值时才渲染<dd> $wineprice_pourright = get_sub_field('wineprice_pourright'); if( $wineprice_pourright ): ?> <dd><?php echo $wineprice_pourright; ?></dd> <?php endif; ?> <?php endwhile; ?> </dl> <?php endif; ?> <?php endwhile; ?> </ul> <?php endif; ?>
核心逻辑就是先获取字段值,判断非空后再输出对应HTML元素,完全基于现有Repeater结构修改,不会影响原有功能的灵活性。
内容的提问来源于stack exchange,提问作者Churchill
相关产品推荐
相关产品推荐

