Support

Account

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

Solved

Getting labels of select field created by php

    • locomo

    • October 25, 2013 at 7:02 am

    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 ..

    • Elliot

    • October 28, 2013 at 4:23 pm

    Hi @locomo

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

    
    <?php 
    
    echo '<pre>';
    	print_r($field);
    echo '</pre>';
    die; ?>
    
    • locomo

    • October 29, 2013 at 2:37 am

    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]

    • Elliot

    • October 29, 2013 at 6:00 pm

    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)

You must be logged in to reply to this topic.

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.