Support

Account

Home Forums Front-end Issues multiple value and get_field_object

Solving

multiple value and get_field_object

    • moud

    • July 5, 2013 at 11:00 pm

    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 ');
    
    • Elliot

    • July 6, 2013 at 11:06 am

    Hi @moud

    Did @wells5609 reply solve your question?

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.