- Web制作
ACF グループフィールドの中で繰り返しフィールドを使う方法

WordPress のプラグイン ACF | Advanced Custom Fields で、グループフィールドの中で繰り返しフィールド(リピーターフィールド)を使用する場合、PHP はどのように書けばいいでしょうか。
下の画像のように ACF を設計したとします。

グループ内にある繰り返しフィールドをループするには、まずグループをループしてから、グループのループ内の繰り返しフィールドをループする必要があるようです。
<?php while (have_rows('group')) : the_row(); while (have_rows('repeat')) : the_row(); // vars $title = get_sub_field('title'); $text = get_sub_field('text'); ?> <div class="title"><?php echo $title; ?></div> <div class="text"><?php echo $text; ?></div> <?php endwhile; endwhile; ?>
こんな感じでコードを書けばバッチリです。

名古屋の Web 制作会社で 9 年半働いた後フリーランスに。中小企業のWEBサイト制作実績 100 サイト以上。ディレクション、デザイン、コーディング、WordPress 構築まで手掛けます。主にWeb系の情報をお届けします。