Support

Account

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

Solved

Show the values ​​of a custom field as icons

  • Hello,
    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

  • 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.

    Cheers
    E

  • 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.

    <?php
    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)

The topic ‘Show the values ​​of a custom field as icons’ is closed to new replies.