7. get_sub_field() and the_sub_field() | Field Forge - Custom Fields, Built for Speed
Download Log in

7. get_sub_field() and the_sub_field()

Developer Guide

Retrieve or echo a sub-field value inside a have_rows() loop. These functions only work within the context of a while ( have_rows() ) : the_row(); loop.

get_sub_field( $selector )

Parameters:
ParameterTypeDefaultDescription
$selectorstringSub-field name
Returns: mixed — the sub-field value.

the_sub_field( $selector )

Parameters:
ParameterTypeDefaultDescription
$selectorstringSub-field name
Returns: void (outputs directly).
php
if ( have_rows( 'features' ) ) :
    echo '<ul>';
    while ( have_rows( 'features' ) ) : the_row();
        echo '<li>';
        echo '<strong>' . esc_html( get_sub_field( 'title' ) ) . '</strong>: ';
        the_sub_field( 'description' );
        echo '</li>';
    endwhile;
    echo '</ul>';
endif;

get_sub_field_object( $selector )

Get the full field definition of a sub-field inside a row loop.

Parameters:
ParameterTypeDefaultDescription
$selectorstringSub-field name
Returns: array|false — field definition array with value, or false if not found.
php
if ( have_rows( 'settings' ) ) :
    while ( have_rows( 'settings' ) ) : the_row();
        $obj = get_sub_field_object( 'color_scheme' );
        // $obj = ['name' => 'color_scheme', 'type' => 'select', 'choices' => [...], 'value' => 'dark']
        echo '<p>Current: ' . esc_html( $obj['value'] ) . '</p>';
        echo '<p>Choices: ' . implode( ', ', $obj['choices'] ) . '</p>';
    endwhile;
endif;

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs