Support

Account

Home Forums Front-end Issues multiple value and get_field_object

Solving

multiple value and get_field_object

  • Hello Elliot
    I would like to retrieve multiple values ​​from a select field.

    I use “get_field_object” for the label and value.

    Everything works fine on the rest except when I select multiple values.
    it makes me a “array”

    Here is my code.

    <?php
                $technologie_de_lecture = "technologie_de_lecture";
                $field = get_field_object($technologie_de_lecture);
                if(get_field('technologie_de_lecture'))
                {
                echo '
                    <div id="container-caracteristiques-fiches-produits">
                        <span id="titre-caracteristiques-fiches-produits">'.$field['label'].'</span><br />
                        '.$field['value'].'
                    </div>
                    ';
                }
            ?>

    thank you very much
    Mathieu

  • @moud,

    You’ll have to loop through array values – for example:

    foreach($field['value'] as $value):
      echo $value . '<br>';
    endforeach;
    

    Or, here’s a function I use often to output a human-readable a comma-separated list:

    // Place in functions.php
    function implode_nice($array, $separator = ', ', $last_separator = ' & ') {
    	if ( count( $array ) == 1 ) {
    		return reset( $array );	
    	}		
    	$end_value = array_pop( $array );				
    	$list = implode( $separator, $array );			
    	return $list . $last_separator . $end_value;			
    }
    
    // Use in template like:
    echo implode_nice($field['value'], ', ', ' and ');
    
  • Hi @moud

    Did @wells5609 reply solve your question?

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

The topic ‘multiple value and get_field_object’ is closed to new replies.