Support

Account

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

Solved

How to get the Select Choice Value

    • Evakos

    • May 21, 2020 at 9:13 pm

    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

    • Evakos

    • May 22, 2020 at 5:37 pm

    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)

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.