Skip to content

Instantly share code, notes, and snippets.

@leoken
Created October 28, 2013 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoken/7190969 to your computer and use it in GitHub Desktop.
Save leoken/7190969 to your computer and use it in GitHub Desktop.
<?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;
 
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment