get_sub_field()

Description

This function will return a sub field value from a repeater field or flexible content field loop. This function is used within a have_rows() loop.

Parameters

<?php get_sub_field($sub_field_name, $format_value); ?>
  • $sub_field_name (string) the name of the field to be retrieved. eg “page_content” (required)
  • $format_value (boolean) whether or not to format the value loaded from the db. Defaults to true (optional).

Usage

Basic (Repeater)

This example shows how to loop through a Repeater field and load a sub field value.

<?php 
    
if( have_rows('parent_field') ):

    while( have_rows('parent_field') ) : the_row();
        
        $value = get_sub_field('sub_field');

    endwhile;

endif;

?>

Basic (Flexible Content)

This example shows how to loop through a Flexible Content field and load a sub field value.

<?php 
    
if( have_rows('parent_field') ):

    while( have_rows('parent_field') ) : the_row();
		
		// get layout
		$layout = get_row_layout();
		
		
		// layout_1
		if( $layout === 'layout_1' ) {
			
			$value = get_sub_field('sub_field_1');
		
		// layout_2	
		} elseif( $layout === 'layout_2' ) {
			
			$value = get_sub_field('sub_field_2');
			
		}

    endwhile;

endif;

?>

Nested

This example shows how to loop through a nested Repeater field and load a sub field value.

<?php 
    
// parent loop
if( have_rows('parent_repeater') ):

    while( have_rows('parent_repeater') ) : the_row();
		
		// vars
		$parent_title = get_sub_field('parent_title');
		
		
		// child loop
		if( have_rows('child_repeater') ):
		
		    while( have_rows('child_repeater') ) : the_row();
			
				// vars
				$child_title = get_sub_field('child_title');
		
			endwhile;
		
		endif;
		// end child loop

    endwhile;

endif;
// end parent loop

?>

Related

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.