Web production note

 【更新日 :

【WordPress】ACFのFlexible Field(柔軟コンテンツ)を出力するコードサンプル

WordPressのプラグインAdvanced Custom Fields PRO (ACF PRO) のFlexible Field(柔軟コンテンツ)の出力コードサンプルです。
中のフィールドはサブフィールドになります。

whileで出力

<?php if(get_field('フィールド名')): ?>
	<?php while(the_flexible_field('フィールド名')): ?>
		
		<?php if(get_row_layout() == 'レイアウト名'): ?>
			
			<?php the_sub_field('コンテツフィールド名'); ?>
		
		<?php endif; ?>
		
	<?php endwhile; ?>
<?php endif; ?>

foreachで出力

<?php 
	$post_set = get_field('フィールド名');
	if($post_set):
?>
	<?php foreach($post_set as $post_set_elm): ?>

		<?php
			$post_set_type = $post_set_elm['acf_fc_layout'];
		?>
		<?php if($post_set_type === 'レイアウト名'): ?>
			<?php 
				$field_val = $post_set_elm['コンテツフィールド名'];
				if($field_val):
			?>
				<?php echo $field_val; ?>
			<?php endif; ?>

		<?php elseif($post_set_type === 'レイアウト名2'): ?>
			<?php 
				$field_val = $post_set_elm['コンテツフィールド名2'];
				if($field_val):
			?>
				<?php echo $field_val; ?>
			<?php endif; ?>
		<?php endif; ?>

	<?php endforeach; ?>
<?php endif; ?>

参考リンク