Support

Account

Home Forums General Issues Label and Value in an array – help needed :(

Solving

Label and Value in an array – help needed :(

  • Hi there

    I’m just starting out creating websites for a school project and need some help… Please 🙂

    I am pulling in data from ACF which prints the label and the value which is a URL.

    Here’s what I’m trying to achieve and works when only one option is selected. It prints the label as the anchor and the value which is the URL.

    <?php the_field(]”><?php $field = get_field_object(‘area’); $value = get_field(‘area’); $label = $field[‘choices’][ $value ]; ?><?php echo $label; ?>

    The problem is how to display data when using “select multiple values”, how would I print those out separated by a comma?

    I have this so far thanks to the support team.

    $field_key = "field_5039a99716d1d";
    $field = get_field_object($field_key);
    
    if( $field )
    {
    		foreach( $field['choices'] as $k => $v )
    		{
    			echo "' . $k . '".',' . $v ;
    		}
    }

    Hope you can help.

    John

    —- Versions —-
    ACF v4.4.3
    WP v4.3.1

  • Hi @johnbell

    Please put all your code in code tags so it’s readable and easier to copy 🙂

  • Thanks Jonathan, I’ve done that.

  • Hi, just wondering if anyone had any thoughts, ideas on this?

    Many thanks.

  • Hi John,

    Try this:

    
    $field = get_field_object('field_5039a99716d1d');
    
    if( $field ){
    	foreach( $field['choices'] as $k => $v ){
    		echo $k . ': ' .  $v;
    	}
    }
    

    If the field is indeed a multi select it should work.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Label and Value in an array – help needed :(’ is closed to new replies.