15th Dec '23
/
0 comments

ACF Repeater Sub Field Data

Looking to get a specific sub field’s data like its label from a specific ACF Repeater?

Here’s a function for that:

<?php 

if ( ! function_exists( 'bl_get_acf_sub_field_object' ) ) {
    function bl_get_acf_sub_field_object( string $field_name, string $repeater ): array {
        $object = [];

        if ( have_rows( $repeater ) ):
            while( have_rows( $repeater ) ): the_row();
                $field_object = get_sub_field_object( $field_name );

                if ( $field_object ) {
                    $object = $field_object;
                }
            endwhile;
        endif;
        
        return $object;
    }
}

Given

Sample usage:

echo bl_get_acf_sub_field_object( 'institution', 'scientific_coordinators' )['label']

(for non-array values)

The list of possible array keys besides label can be found in the doc here.

If you use Bricks, to show the label and the value in the Repeater’s query loop:

The benefit of doing this over hardcoding label text within the Basic Text element’s text, should you need to display the labels, is that now the output will always be in sync with any changes done to the labels in the field group.

Remember that it is not possible to use :array_value Bricks filter for outputting labels without a custom function because ACF Repeater query loop object only contains name and value.

Thanks to Jenn Lee from the Bricks support team for the code snippet.

Get access to all 625 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
ACF Repeater Row Count Condition in Bricks

ACF Repeater Row Count Condition in Bricks

This Pro tutorial provides the steps to output elements on single posts (of any post type) in Bricks only if a specific ACF Repeater-type of…
Categories:
Pro
Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

On product pages, we show all the FAQs associated with the current WooCommerce product's product categories.
Categories:
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Custom Function for ACF Link Field

Custom Function for ACF Link Field

How to output the label of an ACF link field through Dynamic Data.
Categories:
Tags:
Pro
CPT Submenu Items in ACF Pro Options Admin Menu

CPT Submenu Items in ACF Pro Options Admin Menu

This Pro tutorial provides the steps to add links to admin pages under an Options page created with ACF Pro. All code mentioned in this…