You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:31:10