Home Forums Front-end Issues Show the values ​​of a custom field as icons


Show the values ​​of a custom field as icons

    • defharo

    • September 17, 2013 at 9:15 am

    I need help to resolve this issue as I am not a programmer.
    I have a custom field (select or checkbox) called “colors” and 3 values ​​(red, green, black).
    I want to display a custom icon depending on the value chosen.
    I had this piece of code that I worked in earlier versions:

    <?php $value = get_field('colors');
    if($value == "Red") {
    	echo '<span class="red" title="Red"></span>';
    elseif($value == 'Green') {
    	echo '<span class="green" title="Green"></span>';
    elseif($value == 'Black') {
    	echo '<span class="black" title="Black"></span>';

    Thanks for your help

    • Elliot

    • September 17, 2013 at 1:13 pm

    Hi @defharo

    You say select or checkbox. These 2 field types can save data in 2 different ways. Please be specific as to which field type you are using and also if you have any options selected such as multiple values.

    Also, please checkout the select / checkbox documentation. I have a feeling you find some example code which will demonstrate how to use the in_array function you need.


    • defharo

    • September 17, 2013 at 7:46 pm

    Yes, sorry for the confusion of my question.
    I thought it served the same code for both, regardless of the multiple-choice factor.
    But his indication was perfect to find the code I needed.
    It had been several times and select the checkbox documentation but I could not understand.
    Now it works perfect for single or multiple value select and checkbox.

    if( in_array( 'red', get_field('colors') ) )
       echo '<span class="red"></span>';
    if( in_array( 'green', get_field('colors') ) )
       echo '<span class="green"></span>';

    Thanks for your help

Viewing 3 posts - 1 through 3 (of 3 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.