Support

Account

Home Forums General Issues Displaying Labels

Solved

Displaying Labels

  • I have a basic grip on php and can usually get away with a few minutes research when checking how to do a thing. But I’ve stumbled a bit here.

    I’ve got a custom field which allows the author to select some checkboxes when making a post to define the variations available. On the other side the customer can then select a product from those available using a selection box. All good so far.

    I’d like to display the labels of the boxes checked rather than their values and I can’t seem to make it work. I need the values for some js so they could come through as the option ids.

    Here’s what I have:

    <?php
    $values = get_field('product_types');
    {
        echo '<select>';
    
        foreach($values as $value)
        {
            echo '<option>' . $value . '</option>';
        }
    
        echo '</select>';
    }
    
    ?>

    Any help would be greatly appreciated.

  • Try this:

    
    <?php
      $full_object = get_field_object('field_name_here');
      
      foreach($full_object['value'] as $value){
        
        // $value is the 'value'
        $label = $full_object['choices'][$value];
    
    } ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.