Support

Account

Home Forums Front-end Issues Using field names as classes for a

Solved

Using field names as classes for a

  • Hi guys,

    I’m guessing this is very simple but I’m struggling to work out how to do it.

    I’m trying to create a unordered list using the choices selected from checkbox list in ACF. However, I need the choice value inside the list item and the choice name added as the class e.g.

    <ul>
         <li class="red">Red</li>
         <li class="blue">Blue</li>
         <li class="green">Green</li>
         <li class="red">Red</li>
    </ul>

    Checkbox Choices in ACF are setup correctly I believe.

    blue : Blue
    red : Red
    green : Green
    yellow : Yellow

    (Image attached shows the rest of the options I’ve used ACF)

    I am aware this is almost a duplicate of:

    https://support.advancedcustomfields.com/forums/topic/add-value-as-classes-checkbox/

    However, I think what Jeff P suggested is slightly different to this setup (and currently not working for me).

    Been searching for a way to do this for a few hours now, so any help would be greatly appreciated. Many thanks,

    Mike

  • Your Return Value must be set to Both (Array). That way your field will be an array that contains both the value and the name for each choice, which is what you’re looking for.

    Once you have that, try this code out as an example:

    <?php
    $colors = get_field('features');
    if( $colors ): ?>
    <ul>
        <?php foreach( $colors as $color ): ?>
            <li class="<?php echo $color['value']; ?>"><?php echo $color['label']; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
  • Ah I see! I changed to array / added that code and it worked perfectly. Many thanks for taking the time to reply.

  • @mike3deep No problem, glad it worked! But will you please mark my answer above as the post that solved this question so others can find it easier.

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

The topic ‘Using field names as classes for a’ is closed to new replies.