0
votes

I am currently trying to display both the value and the label for Advanced Custom Fields (ACF) by using their documentation here: https://www.advancedcustomfields.com/resources/select/

Currently only the label will display.

In the ACF plugin tool for choices it says:

Enter each choice on a new line.

For more control, you may specify both a value and label like this:

red : Red

So I entered the following:

free : Free License

To display the value and label I entered my field name:

<?php
$field = get_field_object('license_type');
$value = $field['value'];
$label = $field['choices'][ $value ];

echo 'Value: ' . $value . '<br>' . 'Label: ' . $label;
?>

This only displays the value like so:

Value: Free Licence
Label: 

Which seems wrong I believe that should actually be the label.

What I should or I want to display is:

Value: free
Label: Free License

It seems pretty straight forward but I must be doing something wrong.

1

1 Answers

1
votes

What you will need to do is when you define the Select field, select the Return Format as 'Both (Array)'.

This means you can then call your field as a variable and echo the label and value:

$field = get_field('license_type');
$value = $field['value'];
$label = $field['label'];

echo 'Value: ' . $value . '<br>' . 'Label: ' . $label;

Hope this helps.