0

I am using Advanced Custom Fields and the add-on Repeater to create a field in the admin through which a user can select multiple icons. Each icon has an associated label, and I'm trying to return the results of the checked icons in a foreach loop. Here's my code:

<?php
// icons
foreach (get_sub_field('nav_widget_platforms') as $label => $value) {
    echo '<div class="mps-nav-context"><i class="icon-'.$value.'" title="'.$label.'"></i><span>'.$label.'</span></div>';
}
?>

So for example, if the checkbox cloud : eCommerce is selected, the $value should be cloud, and the $label should be eCommerce.

The problem is that $label is returning numbers, not labels, so that the page is displaying 0, 1, etc. depending on which order it is in the array.

I'm not sure what I'm doing wrong here, or how to troubleshoot it.

closed as off-topic by helgatheviking, Milo, Nicolai, fuxia May 13 '14 at 19:06

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.

0

ACF is actually doing what it should - storing a numerical array of selected values. If you want the label you can levy get_field_object, which returns information about the custom field (rather than the value of it for the given post).

$field_obj = get_field_object( 'nav_widget_platforms' ); 
$platforms = get_field( 'nav_widget_platforms' ); 
foreach ( $platforms as $platform ){
    echo $field_obj['choices'][ $platform ] . ": $platform<br />";
}

https://wordpress.stackexchange.com/a/90406/1685

  • How would I apply this solution to my code above? I'm sorry, I don't understand the syntax of arrays well enough to interpret how to use this answer. – user2557385 May 13 '14 at 17:00
  • 1
    I figured it out! The problem was that this is a repeater field, so I had to use get_sub_field_object and get_sub_field instead, but then it worked like a charm. Thanks! – user2557385 May 13 '14 at 17:06

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