4

I am using the wordpress Advanced Custom Fields plugin.

This question is about the select field, and in the documentation only tells me how to output the value, not the label/name.

I can't see this functionally in the documentation and was wondering if anyone had any ideas?

For example, to create Select field content, you simple do this...

gb : English
fr : Français
it : Italiano
de : Deutsch
pt : Português
es : Español
nl : Nederlands
be : Belgian
dk : Dansk
fi : Suomi
no : Norske
cz : Český
pl : Polski
hu : Magyar
ch : Schweiz
at : Österreich
eu : International


Then to output this, you write this...

<?php the_field('language'); ?>


For example if I select Polski in my post editor, the PHP will echo this value...

pl


I'm wondering if its possible to echo the label, I also want to be able to echo Polski, as well as the value pl.

If anyone knows of any documentation on how to do this or if you could help with a solution that would be most awesome.

Thanks in advance.

closed as off-topic by Sisir, cjbj, Dave Romsey, Max Yudin, Fayaz Apr 18 '17 at 7:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is so simple, just set the Return format to Both (Array) in ACF settings for this field. And then, when calling this field in PHP you can do it like this: $lang = get_field('language'); echo $lang['label'] . ' - this is label, ' . $lang['value'] . ' - and this is label.'; – dboris Oct 17 '18 at 19:42
15

The get_field_object() function requires the field KEY not the field NAME. See docs: http://www.advancedcustomfields.com/resources/functions/get_field_object/

So it should looks something like this...

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

You can find the field key by clicking on "Screen Options" > "Show Field Key" and it should appear next to the field type. See attached animated gif-cast below.

Show Field Key

8

Throwing the solution here for better reading.

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

The value in $label will be the label related to the value set in $value.

  • The documentation for ACF is wrong on this part. You need to use the field key for get_field_object. Like so: get_field_object( 'field_5682538b28a71' ); To get your field key, when you're in the ACF edit screen, open Screen Options and enable field keys – Andrew Lazarus Dec 30 '15 at 14:39
  • @AndrewLazarus The key field is no longer required. It works perfectly with the field label. – Jacob Raccuia Oct 31 '17 at 6:14
0

Just in case this helps someone else.

To echo the value of an ACF select field:

$a = get_field_object('field_1234567'); 
echo $a['value'];

(where field_1234567 is the field key obtained by selecting 'Show Field Key' in the Screen Options)

Not the answer you're looking for? Browse other questions tagged or ask your own question.