Support

Account

Home Forums General Issues Getting labels of select field created by php

Solved

Getting labels of select field created by php

  • For some reason I’m having trouble getting at the labels of a select field I’ve created via PHP – the field is called “pay_description” and it is a sub field of a repeater field

    and has a choices array:
    ‘choices’ => array (
    ‘deposit’ => ‘Deposit’,
    ‘payment1’ => ‘1st Payment’,
    ‘payment2’ => ‘2nd Payment’,
    ),

    here’s the code i’m using

    $field = get_field_object('pay_description');
    $value = 'payment1';
    $label = $field['choices'][ $value ];
    echo $label;

    nothing is echoed

    i feel like i’m doing something silly ..

  • Hi @locomo

    Always DEBUG your code when it doesn’t do what you want:

    
    <?php 
    
    echo '<pre>';
    	print_r($field);
    echo '</pre>';
    die; ?>
    
  • ok thanks .. my issue was that I was trying to use “get_field_object” to access a sub field of a repeater

    is it possible to directly access a sub field of a repeater with its key?

    i was able to get to the field i needed without looping through all of the sub_fields like this but i’d rather use the api if there is a cleaner way:
    $payment_object[‘sub_fields’][0]

  • Hi @locomo

    There is a function called get_sub_field_object, however you need to use this within the has_sub_field loop

    Currently, you cannot target a sub field directly by it’s key. This will be possible in v5 thanks to the new data structure!

    Thanks
    E

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

The topic ‘Getting labels of select field created by php’ is closed to new replies.