Description
Returns the value of a specific sub field value from a Repeater or Flexible Content field loop.
Parameters
get_sub_field($selector, [$format_value = true], [$escape_html = false]);
$selector
(string) (Required) The sub field name or field key.$format_value
(bool) (Optional) Whether to apply formatting logic. Defaults to true.$escape_html
(bool) (Optional) Since 6.2.6 – return an escaped HTML safe version of the field value. Requires $format_value to be true. Defaults to false.
Return
(mixed) The sub field value.
Examples
Get a value from within a Repeater field.
This example shows how to loop through a Repeater field and load a sub field value.
if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row();
$sub_value = get_sub_field('sub_field');
// Do something...
endwhile;
else :
// no rows found
endif;
Get a value from within a Flexible Content field.
This example shows how to loop through a Flexible Content field and load a sub field value.
if( have_rows('parent_field') ):
while( have_rows('parent_field') ): the_row();
// Layout 1.
if( get_row_layout() == 'layout_1' ):
// Layout 1 value.
$value = get_sub_field('sub_field_1');
// Layout 2.
elseif( get_row_layout() == 'layout_2' ):
// Layout 2 value.
$value = get_sub_field('sub_field_2');
endif;
endwhile;
endif;
Nested loops
This example shows how to loop through a nested Repeater field and load a sub field value.
/**
* Field Structure:
*
* - parent_repeater (Repeater)
* - parent_title (Text)
* - child_repeater (Repeater)
* - child_title (Text)
*/
if( have_rows('parent_repeater') ):
while( have_rows('parent_repeater') ) : the_row();
// Get parent value.
$parent_title = get_sub_field('parent_title');
// Loop over sub repeater rows.
if( have_rows('child_repeater') ):
while( have_rows('child_repeater') ) : the_row();
// Get sub value.
$child_title = get_sub_field('child_title');
endwhile;
endif;
endwhile;
endif;
Supercharge Your Website With Premium Features Using ACF PRO
Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.
Related
- Functions: the_sub_field()
- Functions: get_sub_field_object()
- Functions: have_rows()
- Deprecated: has_sub_field()
- Basic: get_field_object()