Description

The get_sub_field function is used with the repeater field and the flexible content field (license key required) to return a sub field value. When looping through one of these fields, this function returns a sub field from the current row.

Parameters

<?php get_sub_field($sub_field_name); ?>
  • $sub_field_name: the name of the field to be retrieved. eg “page_content” (required)

Usage

<?php
 
/*
*  Loop through a Repeater field
*/
 
if( get_field('repeater') )
{
	while( has_sub_field('repeater') )
	{ 
		$variable = get_sub_field('sub_field');
 
		// do something with sub field...
	}
}
 
/*
*  Loop through a Flexible Content field
*/
 
if( get_field('flexible_content') )
{
	while( has_sub_field("flexible_content") )
	{
		if( get_row_layout() == "paragraph" ) // layout: Paragraph
		{
			$variable = get_sub_field('sub_field');
 
			// do something with sub field...
		}
		elseif( get_row_layout() == "file" ) // layout: File
		{
			$variable = get_sub_field('sub_field');
 
			// do something with sub field...
		}
	}
}
 
/*
*  Loop through nested Repeater fields
*/
 
if( get_field('parent_repeater') ): ?>
	<?php while( has_sub_field('parent_repeater') ): ?>
 
		<div>
			<?php if( get_sub_field('child_repeater') ): ?>
				<ul>
				<?php while( has_sub_field('child_repeater') ): ?>
					<li>
						<?php 
 
						$variable = get_sub_field('sub_field');
 
						// do something with sub field... 
 
						?>
					</li>
				<?php endwhile; ?>
				</ul>
			<?php endif; ?>
		</div>	
 
	<?php endwhile; ?>
<?php endif; 
 
/*
*  Loop through nested Repeater fields (From another $post ID)
*/
 
if( get_field('parent_repeater', 123) ): ?>
	<?php while( has_sub_field('parent_repeater', 123) ): ?>
 
		<div>
			<?php 
 
			/*
			*  note: you don't need to specify the $post_id for any sub field functions
			*/
 
			if( get_sub_field('child_repeater') ): ?>
				<ul>
				<?php while( has_sub_field('child_repeater') ): ?>
					<li>
						<?php 
 
						$variable = get_sub_field('sub_field');
 
						// do something with sub field... 
 
						?>
					</li>
				<?php endwhile; ?>
				</ul>
			<?php endif; ?>
		</div>	
 
	<?php endwhile; ?>
<?php endif;
 
?>

 Related Documents