ACFで例えば 連番で以下のような設定をした時の効率的な出力方法
「cf_hoge_title01(テキスト)」「cf_hoge_image01(画像_配列)」「cf_hoge_text01(URL)」「cf_hoge_url01(テキスト)」
「cf_hoge_title02」「cf_hoge_image02」「cf_hoge_text02」「cf_hoge_url02」
「cf_hoge_title03」「cf_hoge_image03」「cf_hoge_text03」「cf_hoge_url03」
「cf_hoge_title04」「cf_hoge_image04」「cf_hoge_text04」「cf_hoge_url04」
「cf_hoge_title05」「cf_hoge_image05」「cf_hoge_text05」「cf_hoge_url05」
5つのグループの内容が繰り返されているような場合
繰り返しフィールドは使いません。
<?php
for ( $i = 1; $i <= 5; $i++ ) :
$n = sprintf( '%02d', $i );
?>
<?php the_field( 'cf_hoge_title' . $n ); ?>
<?php
$image_key = 'cf_hoge_image' . $n;
$image = get_field( $image_key );
if ( ! $image ) { continue; }
?>
<img src=<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" width="" height="" />
<?php the_field( 'cf_hoge_text' . $n ); ?>
<?php the_field( 'cf_hoge_url' . $n ); ?>
<?php endfor; ?>フィールド名の連番を利用しfor文で出力
上記は5回の繰り返しですが、回数を変更する場合は2行目の数字の5を変更する
画像が入っていない場合はそのグループの出力全てをキャンセルする場合
<?php
for ( $i = 1; $i <= 5; $i++ ) :
$n = sprintf( '%02d', $i );
$image_key = 'cf_hoge_image' . $n;
$image = get_field( $image_key );
// 画像がなければ表示しない
if ( ! $image ) {
continue;
}
?>
<?php the_field( 'cf_hoge_title' . $n ); ?>
<img src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" width="" height="" />
<?php the_field( 'cf_hoge_text' . $n ); ?>
<?php the_field( 'cf_hoge_url' . $n ); ?>
<?php endfor; ?>

