Support

Account

Home Forums Front-end Issues Value return number(index) instead word's

Solving

Value return number(index) instead word's

  • Hello support!
    I’ve try to show multiselect and checkbox values , if i try it with standart code( example for multiselect <?php implode(‘,’, get_field(‘value’)); ?> ) it return numbers value ( ex. 0,3,4). as i see it index
    my values write as
    0:value
    1:value

    One select field with this code working good:
    <?php $field = get_field_object('ceiling');$value = get_field('ceiling'); print $field['choices'][ $value ]; ?>
    but standart code <?= get_field(‘price’) ?> -return numbers only

    I’ve need code to show multiselect field!please help! thanks

    sor for my english)

  • Hi @mczmike

    So if I understand you correctly you want to display the values from a multiselect. Have you tried taking a look at what your get_field function returns using print_r()?.

    You should be able to do a foreach loop like:

    
    $values = get_field('myfield');
    foreach( $values as $value ){
    	echo $value;
    
    }
    
    
  • hello!
    print_r() is returned array ex.
    [0] => 0 [1] => 2
    what i need to show words?

  • Hi @mczmike

    I feel like the issue here is in the way you’ve set up the select values.
    In the field settings, have you setup the values like

    
    0 : value1
    1 : value2
    etc.
    

    ?

    get_field will only return the value part of the select (0,1 etc) and not the label you set (value1, value2 etc.).

  • ok! how to show label (value1,value2..)???

  • i’ve found this code for checkbox, my field named “engineering”:

    <div><?= $field = get_field_object('engineering');
    $value = $field['value'];
    $choices = $field['choices'];
    if( $value ):?>
    <?php foreach( $value as $v ): ?> , <?php echo $choices[ $v ]; ?><?php endforeach; ?>.<?php endif; ?> </div>

    in result i see:

    Array, value1, value2.
    why i see “array”?

  • Because the single choices value is not a string but an array of values itself.
    http://www.advancedcustomfields.com/resources/get_field_object/

    So you’d need something like:

    
    <div>
    	<?php $field = get_field_object('engineering');
    	$value = $field['value'];
    	$choices = $field['choices'];
    	if( $value ):
    		foreach( $value as $v ):
    			echo $choices[$v]['label'];
    		endforeach;
    		echo '.';
    	endif; 
    	?> 
    </div>
    
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Value return number(index) instead word's’ is closed to new replies.