Support

Account

Home Forums Front-end Issues Show squares with select field colors as background

Solving

Show squares with select field colors as background

  • Hello people, everything fine? I’m trying to make me display the colors of the select field as background in squares, I’m trying to use the following code:

    $colors = get_field( 'cor' );
    
    // Create a comma-separated list from selected values.
    if( $colors ): ?>
    {
    <span style="height: 20px; width: 20px; margin-right: 3px; position: relative; background-color: <?= implode( ', ', $colors ); ?>;"></span>
    }
    <?php endif; ?>

    But I’m not having success, when inspecting the page it appears that it can get the colors, but it doesn’t show the squares on the front end, could you help me? Please!

    I’ll attach some prints I took for you to see how it is and how I need it

  • Detail: I’m using Divi theme + ACF and using WPCode to insert a shortcode in the divi code module

  • 
    $colors = get_field( 'cor' );
    
    // Create a comma-separated list from selected values.
    if ($colors) {  
      $boxes = array();
      foreach ($colors as $color) {
        $boxes[] = '<span style="height: 20px; width: 20px; margin-right: 3px; position: relative; background-color: '.$color.';"></span>';
      }
      echo implode(', ', $boxes);
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.