ACF Flexible Content Field – Layout Labels and Sub Field Labels

Updated on 28 Jul 2024

This Pro tutorial provides the steps to output the text of layout labels and/or sub field labels when using the Flexible Content field of Advanced Custom Fields Pro.

Consider this sample field group:

Generally speaking, it is okay to hardcode these texts, but there could be some situations where you want to pull the labels dynamically. This means that if the labels are changed in the field group, the labels will automatically update on the front end without having to manually change the labels in the template/builder to match the updated text.

Update: A JSON export of the field group and JSON code of the fully-built Section from our dev site are provided near the end.

Layout Labels

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

function bl_get_flexible_content_layout_labels( string $field_name, string $layout_name = '' ): array|string|null {
    $field = get_field_object( $field_name );
    $layouts = [];

    if ( $field && $field['type'] === 'flexible_content' && isset( $field['layouts'] ) ) {
        foreach ( $field['layouts'] as $layout ) {
            $layouts[$layout['name']] = $layout['label'];
        }
    }

    if ( ! empty( $layout_name ) ) {
        return isset( $layouts[$layout_name] ) ? $layouts[$layout_name] : null;
    }

    return $layouts;
}

Usage:

<?php

echo bl_get_flexible_content_layout_labels( 'page_content', 'text_image' )

?>

where page_content is the Flexible Content field name and text_image is the layout name.

outputs: Text Image (layout label)

In Bricks:

Whitelist the bl_get_flexible_content_layout_labels function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_flexible_content_layout_labels'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_flexible_content_layout_labels. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

{echo:bl_get_flexible_content_layout_labels(page_content,text_image)}

where page_content is the Flexible Content field name and text_image is the layout name.

If you would like to be dynamic so the layout name is automatically taken for a heading/text element inside the ACF Flexible Content query loop, you could instead use:

{echo:bl_get_flexible_content_layout_labels(page_content,{acf_get_row_layout})}

If the second argument is not provided in the function call, an associative array having layout names as the keys and layout labels as values will be returned.

Field Labels

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

function bl_get_flexible_content_field_label( string $flexible_content_name, string $layout_name, string $field_name ): ?string {
    $field_object = get_field_object( $flexible_content_name );
    
    if ( ! $field_object || $field_object['type'] !== 'flexible_content' || ! isset( $field_object['layouts'] ) ) {
        return null;
    }

    foreach ( $field_object['layouts'] as $layout ) {
        if ( $layout['name'] === $layout_name && isset( $layout['sub_fields'] ) ) {
            foreach ( $layout['sub_fields'] as $sub_field ) {
                if ( $sub_field['name'] === $field_name ) {
                    return $sub_field['label'];
                }
            }
        }
    }

    return null;
}

Usage:

<?php

echo bl_get_flexible_content_field_label( 'page_content', 'image_text', 'image' );

where page_content is the Flexible Content field name, image_text is the layout name and image is the field name.

In Bricks:

Whitelist the bl_get_flexible_content_field_label function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_flexible_content_field_label'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_get_flexible_content_field_label. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

{echo:bl_get_flexible_content_field_label(page_content,image_text,image)}

JSON

Field group

Bricks Section


Sample output of get_field_object( ‘page_content’ )
Array
(
    [ID] => 140
    [key] => field_66a237c7654bc
    [label] => Page Content
    [name] => page_content
    [aria-label] => 
    [prefix] => acf
    [type] => flexible_content
    [value] => Array
        (
            [0] => Array
                (
                    [acf_fc_layout] => text_image
                    [text] => 
Occaecat deserunt fugiat olore consequat. Consequat aliqua dolore culpa. Ut ex in aute quis veniam aliquip nostrud deserunt eiusmod. Aliquip commodo amet enim et cillum in veniam eu qui. Magna nisi reprehenderit dolore cillum occaecat excepteur. Incididunt veniam labore eiusmod non mollit enim est ex dolor sint veniam sit eu tempor. Culpa culpa occaecat fugiat.



                    [image] => Array
                        (
                            [ID] => 57
                            [id] => 57
                            [title] => 1f95f496-90c8-3ed0-bac5-dc0072926f47
                            [filename] => 1f95f496-90c8-3ed0-bac5-dc0072926f47.jpg
                            [filesize] => 209460
                            [url] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47.jpg
                            [link] => https://playground.wpdemo.us/quod-illum-delectus-porro-fugit-explicabo-distinctio/1f95f496-90c8-3ed0-bac5-dc0072926f47/
                            [alt] => 
                            [author] => 0
                            [description] => 
                             => 
                            [name] => 1f95f496-90c8-3ed0-bac5-dc0072926f47
                            [status] => inherit
                            [uploaded_to] => 55
                            [date] => 2023-04-15 06:48:27
                            [modified] => 2023-04-15 06:48:27
                            [menu_order] => 0
                            [mime_type] => image/jpeg
                            [type] => image
                            [subtype] => jpeg
                            [icon] => https://playground.wpdemo.us/wp-includes/images/media/default.png
                            [width] => 1243
                            [height] => 828
                            [sizes] => Array
                                (
                                    [thumbnail] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47-150x150.jpg
                                    [thumbnail-width] => 150
                                    [thumbnail-height] => 150
                                    [medium] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47-300x200.jpg
                                    [medium-width] => 300
                                    [medium-height] => 200
                                    [medium_large] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47-768x512.jpg
                                    [medium_large-width] => 768
                                    [medium_large-height] => 512
                                    [large] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47-1024x682.jpg
                                    [large-width] => 1024
                                    [large-height] => 682
                                    [1536x1536] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47.jpg
                                    [1536x1536-width] => 1243
                                    [1536x1536-height] => 828
                                    [2048x2048] => https://playground.wpdemo.us/wp-content/uploads/2023/04/1f95f496-90c8-3ed0-bac5-dc0072926f47.jpg
                                    [2048x2048-width] => 1243
                                    [2048x2048-height] => 828
                                )

                        )

                )

            [1] => Array
                (
                    [acf_fc_layout] => image_text
                    [image] => Array
                        (
                            [ID] => 78
                            [id] => 78
                            [title] => 35350482-1416-3460-91bb-04534f59e8aa
                            [filename] => 35350482-1416-3460-91bb-04534f59e8aa.jpg
                            [filesize] => 75081
                            [url] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa.jpg
                            [link] => https://playground.wpdemo.us/nam-et-sed-id-nemo/35350482-1416-3460-91bb-04534f59e8aa/
                            [alt] => 
                            [author] => 0
                            [description] => 
                             => 
                            [name] => 35350482-1416-3460-91bb-04534f59e8aa
                            [status] => inherit
                            [uploaded_to] => 76
                            [date] => 2023-04-15 06:49:16
                            [modified] => 2023-04-15 06:49:16
                            [menu_order] => 0
                            [mime_type] => image/jpeg
                            [type] => image
                            [subtype] => jpeg
                            [icon] => https://playground.wpdemo.us/wp-includes/images/media/default.png
                            [width] => 1359
                            [height] => 906
                            [sizes] => Array
                                (
                                    [thumbnail] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa-150x150.jpg
                                    [thumbnail-width] => 150
                                    [thumbnail-height] => 150
                                    [medium] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa-300x200.jpg
                                    [medium-width] => 300
                                    [medium-height] => 200
                                    [medium_large] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa-768x512.jpg
                                    [medium_large-width] => 768
                                    [medium_large-height] => 512
                                    [large] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa-1024x683.jpg
                                    [large-width] => 1024
                                    [large-height] => 683
                                    [1536x1536] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa.jpg
                                    [1536x1536-width] => 1359
                                    [1536x1536-height] => 906
                                    [2048x2048] => https://playground.wpdemo.us/wp-content/uploads/2023/04/35350482-1416-3460-91bb-04534f59e8aa.jpg
                                    [2048x2048-width] => 1359
                                    [2048x2048-height] => 906
                                )

                        )

                    [text] => 
Duis aliquip magna veniam qui minim velit ex cupidatat non. Cillum nisi irure amet ad duis voluptate. In excepteur esse enim ad. Anim mollit aliquip nostrud mollit culpa qui quis deserunt. Occaecat ad est mollit eu minim occaecat cillum quis aute consectetur culpa pariatur nulla proident. Dolor elit labore eu esse in ea ut culpa qui cupidatat minim ex aliquip excepteur sunt. Nostrud enim nulla id sint do deserunt laborum proident consectetur Lorem enim eu magna nostrud culpa. Ullamco proident Lorem officia.



                )

        )

    [menu_order] => 0
    [instructions] => 
    [required] => 0
    [id] => 
    [class] => 
    [conditional_logic] => 0
    [parent] => 139
    [wrapper] => Array
        (
            [width] => 
            [class] => 
            [id] => 
        )

    [layouts] => Array
        (
            [layout_66a237e6907a2] => Array
                (
                    [key] => layout_66a237e6907a2
                    [name] => image_text
                    [label] => Image Text (layout label)
                    [display] => block
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 141
                                    [key] => field_66a238ed654bd
                                    [label] => Image (field label)
                                    [name] => image
                                    [aria-label] => 
                                    [prefix] => acf
                                    [type] => image
                                    [value] => 
                                    [menu_order] => 0
                                    [instructions] => 
                                    [required] => 0
                                    [id] => 
                                    [class] => 
                                    [conditional_logic] => 0
                                    [parent] => 140
                                    [wrapper] => Array
                                        (
                                            [width] => 
                                            [class] => 
                                            [id] => 
                                        )

                                    [parent_layout] => layout_66a237e6907a2
                                    [return_format] => array
                                    [library] => all
                                    [min_width] => 
                                    [min_height] => 
                                    [min_size] => 
                                    [max_width] => 
                                    [max_height] => 
                                    [max_size] => 
                                    [mime_types] => 
                                    [preview_size] => medium
                                    [_name] => image
                                    [_valid] => 1
                                )

                            [1] => Array
                                (
                                    [ID] => 142
                                    [key] => field_66a238f5654be
                                    [label] => Text (field label)
                                    [name] => text
                                    [aria-label] => 
                                    [prefix] => acf
                                    [type] => wysiwyg
                                    [value] => 
                                    [menu_order] => 1
                                    [instructions] => 
                                    [required] => 0
                                    [id] => 
                                    [class] => 
                                    [conditional_logic] => 0
                                    [parent] => 140
                                    [wrapper] => Array
                                        (
                                            [width] => 
                                            [class] => 
                                            [id] => 
                                        )

                                    [parent_layout] => layout_66a237e6907a2
                                    [default_value] => 
                                    [tabs] => all
                                    [toolbar] => full
                                    [media_upload] => 1
                                    [delay] => 0
                                    [_name] => text
                                    [_valid] => 1
                                )

                        )

                    [min] => 
                    [max] => 
                )

            [layout_66a241e8654bf] => Array
                (
                    [key] => layout_66a241e8654bf
                    [name] => text_image
                    [label] => Text Image (layout label)
                    [display] => block
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [ID] => 143
                                    [key] => field_66a241f4654c1
                                    [label] => Text (field label)
                                    [name] => text
                                    [aria-label] => 
                                    [prefix] => acf
                                    [type] => wysiwyg
                                    [value] => 
                                    [menu_order] => 0
                                    [instructions] => 
                                    [required] => 0
                                    [id] => 
                                    [class] => 
                                    [conditional_logic] => 0
                                    [parent] => 140
                                    [wrapper] => Array
                                        (
                                            [width] => 
                                            [class] => 
                                            [id] => 
                                        )

                                    [parent_layout] => layout_66a241e8654bf
                                    [default_value] => 
                                    [tabs] => all
                                    [toolbar] => full
                                    [media_upload] => 1
                                    [delay] => 0
                                    [_name] => text
                                    [_valid] => 1
                                )

                            [1] => Array
                                (
                                    [ID] => 144
                                    [key] => field_66a243c5654c2
                                    [label] => Image (field label)
                                    [name] => image
                                    [aria-label] => 
                                    [prefix] => acf
                                    [type] => image
                                    [value] => 
                                    [menu_order] => 1
                                    [instructions] => 
                                    [required] => 0
                                    [id] => 
                                    [class] => 
                                    [conditional_logic] => 0
                                    [parent] => 140
                                    [wrapper] => Array
                                        (
                                            [width] => 
                                            [class] => 
                                            [id] => 
                                        )

                                    [parent_layout] => layout_66a241e8654bf
                                    [return_format] => array
                                    [library] => all
                                    [min_width] => 
                                    [min_height] => 
                                    [min_size] => 
                                    [max_width] => 
                                    [max_height] => 
                                    [max_size] => 
                                    [mime_types] => 
                                    [preview_size] => medium
                                    [_name] => image
                                    [_valid] => 1
                                )

                        )

                    [min] => 
                    [max] => 
                )

        )

    [min] => 
    [max] => 
    [button_label] => Add Row
    [_name] => page_content
    [_valid] => 1
)

Reference

https://www.advancedcustomfields.com/resources/get_field_object/