Home Forums General Issues Displaying a select field?


Displaying a select field?

  • Okay, I’ve read the documentation on this but for some reason either I’m not getting it or it’s just not working.

    On one of my WordPress themes template files I want to output the value of a select field I have setup that has several choices.

    This is the code I am using:

    $field = get_field_object('intro_text_shadow_color');
    $value = get_field('intro_text_shadow_color');
    $label = $field['choices'][ $value ];
    <p><p style="<?php echo $label; ?>">Intro Text</p></p>

    My select field I have setup on the backend has a label of intro_text_shadow_color.

    That field has the following choices:

    grey : Grey
    black : Black
    light_green : Light Green
    dark_green : Dark Green
    light_brown : Light Brown
    dark_brown : Dark Brown

    So, basically I was hoping my PHP code would output the value of each choice. So for instance, if someone chose ‘Light Green’ it would output ‘light_green’ in my code.

    Any idea why this isn’t working? It’s confusing because according to the documentation I’m using the right code (I think?).

  • Is it not adding anything to the code? Can you look at the source and see what is being output and post that?

  • How strange. I’ve just been having the exact same issue. From reading the select resource page if you have a value and label it should be the value that’s displayed in the template. i.e. we should only need to add all of the code above if you want to display the label. However the value is not displaying either way.

  • A select field actually returns the value and not the label, left side of

    light_green : Light Green

    In this instance, if I wanted to output Light Green I would not include both side in my select field choices. Unless you have plans to use that value for something else.

    So instead of what you have for choices I would simply use:

    Light Green
    Dark Green
    Light Brown
    Dark Brown

    And then the code to show it is simplified to:

    <p style=”color:<?php the_field(‘intro_text_shadow_color’); ?>”>Intro Text</p>

  • Hi John. This is what we’re both trying to say. Using the original poster’s example:

    This code:

    <?php echo the_field('intro_text_shadow_color'); ?>

    Should be displaying the value i.e. light_green

    However it’s not and I’m having the same issue. It’s displaying the Label and not the value.

  • Yes, the code doesn’t work for me at all. When I view the source code my above code simply outputs:

    <p class"">Intro Text</p>

    It should be outputting:

    <p class"light_green">Intro Text</p>

    That way I can apply CSS styling to the ‘light_green’ class. Make sense?

  • This looks like a bug. I am using the Flexible Content field and if I use it as a Select Field from that then it doesn’t work. However, if I have a Select field within a Repeater field in the Flexible Content section then it works as it should.

  • Umm, mine seems to be working correctly now. I did just update to ACF Pro 5.2.7 from 5.2.6 and initially it didn’t work so I just left it, came back and it now works?! Very odd.

  • I’m not using PRO yet, but I am using the most current version of ACF and it doesn’t work at all.

    I’m following the ‘select’ documentation to a T but the code simply doesn’t work. So either this is a bug or the code is somehow wrong.

  • Sorry to leave you hanging on this, for some reason my notifications on this thread got turned off.

    I just did a test of this in ACF 4 and it worked as expected. I used your choices in a select field. I named the select field color

    grey : Grey
    black : Black
    light_green : Light Green
    dark_green : Dark Green
    light_brown : Light Brown
    dark_brown : Dark Brown

    I then created a page that used the select field and selected Light Green

    I then added this to my page template the_field('color');

    When I viewed the page light_green as I expected.

    If you are still having problems with this, please disable all plugins and switch to the default WP theme and try again. It should be working as I described and if it isn’t then you more than likely have a conflict of some kind.

  • I tried a lot of things, but I still can’t get this to work.
    Followed the documentation couple of times, but no success.

    I’m using the PRO version 5.2.8.
    Any help would be grand!

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

The topic ‘Displaying a select field?’ is closed to new replies.