Support

Account

Home Forums Front-end Issues Looping multiple select values and fields

Solved

Looping multiple select values and fields

  • I’m using a wordpress plugin names “Advanced Custom Fields”. Within the admin I have a select field where multiple choices can be selected.

    So for example the admin will have the following selected….

    /apples-category/ : Read More on apples
    /pears-category/ : Read More on pears
    /oranges-category/ : Read More on oranges
    /peach-category/ : Read More on peaches

    The first part which is the value (/apples-category/) will be used within a hyper link. The second part which is the label (Read More on apples) will be the anchor text.

    I’ve put the following together which outputs the following correct labels but for some reason they all have the href (value) of the very last option (/peach-category/)

    Read More on apples, Read More on pears, Read More on oranges, Read More on peaches

    Here’s my code and apologies if it looks obvious, I’ve just started out coding and still need assistance 🙂

    Hope you can assist.

     <?php
     $labels = array();
     $field = get_field_object('select_fruit');
     $values = get_field('select_fruit');
     foreach ($values as $value) {
     $labels[] = $field['choices'][ $value ];
     }
    
     foreach ($labels as $k=>$label){
     echo '<a href="'.$value.'" />'.strtoupper($label).'</a>'.($k ==                 count($labels) - 1 ? '' : ', ');
    }?>
  • OK, I figured it out myself 🙂

    <?php
     $fruit_array = array();
     $field = get_field_object('select_fruit');
     foreach ($field['value'] as $value)
     {
        $fruit_array[$value]=$field['choices'][ $value ];
     }
     $cnt=0;
     foreach ($fruit_array as $fruit_key=>$fruit_value)
     {
        echo '<a href="'.$fruit_key.'" />'.strtoupper($fruit_value).'</a>'.($cnt == count($fruit_array) - 1 ? '' : ', ');
        $cnt++;
     }
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Looping multiple select values and fields’ is closed to new replies.