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 611 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Conditional Output based on Date Time Picker Field in Bricks

Conditional Output based on Date Time Picker Field in Bricks

In the past, we showed how elements can be conditionally output based on a post's Date type of ACF field here. This Pro tutorial for…
Categories:
Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories:
Pro
Random images from ACF Options Gallery in Bricks

Random images from ACF Options Gallery in Bricks

This Pro tutorial provides the steps to output images from an ACF Gallery-type custom field in a Bricks query loop randomly with each image appearing…
Categories:
Pro
Related Services grouped by Service Categories on Single Branch Posts in Bricks

Related Services grouped by Service Categories on Single Branch Posts in Bricks

How to query ACF relationship fields on single CPT templates.
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: