Support

Account

Home Forums Add-ons Repeater Field Show select field label within nested repeater

Solved

Show select field label within nested repeater

  • Hello,

    I have a nested repeater field (repeater inside another repeater) with a ‘select’ field inside that, and I basically just want to output the label for each item in the select field.

    I have read the documentation, which shows the following:

    $field = get_field_object('field_name');
    $value = get_field('field_name');
    $label = $field['choices'][ $value ];

    However I can’t get this to work within a nested repeater. Is there a way around this?

    For info, I am using ACF v3.5

    Any help much appreciated
    James

  • Hi @wickywills

    The get_field_object function only works with parent fields. To find a sub field object, please use the get_sub_field_object function.

    This function is documented below and was added in v4:
    http://www.advancedcustomfields.com/resources/functions/get_sub_field_object/

    Perhaps for now, you could use a work around like so:

    
    <?php 
    
    $field = get_field_object('repeater_field_name');
    
    $sub_fields = $field['sub_fields'];
    
    // debug variable to find the sub field key ($key)
    echo '<pre>';
    	print_r($sub_fields);
    echo '</pre>';
    die;
    
    $sub_field = $sub_fields[ $key ];
    
    ?>
    
  • Hi @elliot,

    Thank you for replying. I should point out that my PHP knowledge isn’t that great(!). I did however get the following output:

    Array
    (
        [field_51e6a66b8509b] => Array
            (
                [label] => Client name
                [name] => client_name
                [type] => text
                [instructions] => 
                [column_width] => 
                [default_value] => 
                [formatting] => html
                [order_no] => 0
                [key] => field_51e6a66b8509b
            )
    
        [field_51e6a6788509c] => Array
            (
                [label] => Client logo
                [name] => client_logo
                [type] => image
                [instructions] => 
                [column_width] => 
                [save_format] => url
                [preview_size] => thumbnail
                [order_no] => 1
                [key] => field_51e6a6788509c
            )
    
        [field_51e6a6878509d] => Array
            (
                [label] => Images
                [name] => images
                [type] => repeater
                [instructions] => 
                [column_width] => 
                [sub_fields] => Array
                    (
                        [field_51e6a69f8509e] => Array
                            (
                                [label] => Large image
                                [name] => large_image
                                [type] => image
                                [instructions] => 
                                [column_width] => 
                                [save_format] => url
                                [preview_size] => thumbnail
                                [order_no] => 0
                                [key] => field_51e6a69f8509e
                            )
    
                        [field_51e9070105abe] => Array
                            (
                                [choices] => Array
                                    (
                                        [kitchen] => Kitchen
                                        [lounge] => Lounge
                                        [hallway] => Hallway
                                        [meeting_room] => Meeting Room
                                        [bathroom] => Bathroom
                                        [reception] => Reception
                                        [bedroom] => bedroom
                                    )
    
                                [label] => Location
                                [name] => location
                                [type] => select
                                [instructions] => 
                                [column_width] => 
                                [default_value] => 
                                [allow_null] => 0
                                [multiple] => 0
                                [order_no] => 1
                                [key] => field_51e9070105abe
                            )
    
                    )
    
                [row_min] => 0
                [row_limit] => 
                [layout] => table
                [button_label] => Add Row
                [order_no] => 2
                [key] => field_51e6a6878509d
            )
    
    )

    What I basically want is to output the ‘choices’ array from the above. I’ve taken a look into multidimensional arrays and understand them (ish!), but not when they’re this deep. Do you know how I can output the ‘choices’?

    Thanks again

  • Hi @wickywills

    
    $sub_field = $sub_fields[ 'field_51e6a6878509d' ][ 'sub_fields' ][ 'field_51e9070105abe' ];
    
  • Ah I get it! Didn’t think it would be that easy 🙂

    Thanks very much @elliot

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Show select field label within nested repeater’ is closed to new replies.

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.