Support

Account

Home Forums Front-end Issues How to get the Select Choice Value

Solved

How to get the Select Choice Value

  • Hi,

    I want to try and get the value of the choice options rather than what is being output. So my field choices look like this:

    oi : Organic Ingredients
    v : Vegan

    I want to get the ‘value’ part of this, not the ‘label’ and output that, but none of what I tried worked. This is my code that works, but it outputs the label in the URL which would work fine but it’s untidy.

    function show_product_info() {
    	$image_url='';
    	$choices = get_field('product_information');
        // loop
        if( $choices) {
            
            foreach( $choices as $key => $image_url) {
                ?>
                <img src="<?php echo get_stylesheet_directory_uri(); ?>/imgs/products/<?php echo  $image_url ?>.svg " />
                <?php
            }	
        }		
    }

    If anyone could give a hint at what I’ve missed there that would be great.

    Chris

  • Hi,

    I figured it out. Should be like this:

    $choices = get_field('product_information');
        // loop
        if( $choices): ?> 
    		<ul class='prod-row'>
           <?php foreach( $choices as $choice): ?> 
               <li> <img src="<?php echo get_stylesheet_directory_uri(); ?>/imgs/products/<?php echo $choice ?>.svg " /> </li>
                <?php endforeach; ?>
    			</ul>
    <?php endif; 

    I hope that helps someone.

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

The topic ‘How to get the Select Choice Value’ is closed to new replies.