Custom Post Type Field Array parsing [closed]
-
11-12-2020 - |
Question
I have a CPT Field named 'course_location' as a checkbox with values. When I use
$field = get_field_object('course_location');
'<pre>searchDateArray'; print_r($field); echo '</pre>';
Which results in the following array
Array (
[key] => field_56056d11c5dd6
[label] => Course Location
[name] => course_location
[_name] => course_location
[type] => checkbox
[order_no] => 3
[instructions] =>
[required] => 0
[id] => acf-field-course_location
[class] => checkbox
[conditional_logic] => Array (
[status] => 0
[rules] => Array (
[0] => Array (
[field] => null
[operator] => ==
)
)
[allorany] => all
)
[choices] => Array (
[orange-county] => Orange County
[los-angeles-county] => Los Angeles County
[test] => Test
)
[default_value] =>
[layout] => vertical
[field_group] => 29
[value] => Array (
[0] => test
)
)
How do I get get the [choices] Array and iterate through the 'choices' and echo out?
<li>$label and $name</li>
Output : orange-county and Orange County
Output : los-angeles-county and Los Angeles County
Output : test and Test
Solution
This will get you the choices array inside $field
variable:
$choices = $field[ 'choices' ];
From there, you can iterate with either a while or foreach loop depending on what you are trying to do with the array.
EDIT: rereading your question, you specify what you are trying to do! Sorry for skipping that part!
echo '<ul>';
foreach($choices as $key => $choice):
echo '<li>' . $key . ' and ' . $choice . '</li>';
endforeach;
echo '</ul>';
Should produce:
- orange_county and Orange County
- los-angeles and Los Angeles
This should not be considered complete code. Escape your output for security's sake. Just illustrating a concept here.