Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:51:04+00:00 2026-06-01T02:51:04+00:00

I’m working with a WordPress Theme. It’s a product showcase and my client needs

  • 0

I’m working with a WordPress Theme. It’s a product showcase and my client needs me to add a function to represent visually the available colors for each product in a small “square” (div) with the background-color css property set to that colour.

In wordpress, I set each product post to have a custom meta field called “colorStock” where they can list the available colours in a comma separated list.

Something like this:

  rojo, azul, verde, amarillo

The list is in spanish and with colour names to make things much easier for the not-savvy users who will admin this page.

So, what I want to do is a function that will take these meta field values, put them in an array and perform different actions for each item.

Why? Well, because these values will be used to set the css background-color for little divs that will represent the colours.

So, what I need to do is to take each value, let’s say “rojo”, and make the function check if rojo is in a list of available (“supported”) colours and output it as the valid color name or value related to rojo. (e.g. "rojo = "red" or "rojo" = "#f5f5f5").

So far I have this code:

function colorStock(){
    $postID = get_the_ID();
    $colorStock = get_post_meta($postID, 'colorStock', true); //Get Colours available
    $myArray = explode(',', $colorStock);
    //print_r($myArray); 
//echo $myArray;


     foreach ($myArray as $item){

        if (in_array("rojo", $item)) {
            echo "red";
        }
        if (in_array("verde", $item)) {
            echo "green";
        }
        if (in_array("azul", $item)) {
            echo "blue";
        }
        if (in_array("blanco", $item)) {
            echo "white";
        }
        if (in_array("negro", $item)) {
            echo "black";
        }
        if (in_array("amarillo", $item)) {
            echo "yellow";
        }
    }      
   }

I know that my in_array usage might not make sense at all, but the thing is that I need the function to take each array item, see if it matches any of the equivalences list items and then move to the next array item. So i will be able to output an individual div for each item, regardless if the admin listed 1 or 20 colours.

Any ideas?


EDIT:

By the way: $colorStockoutputs:
rojo, verde, azul, amarillo

and $myArrayoutputs:
Array ( [0] => rojo [1] => verde [2] => azul )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:51:06+00:00Added an answer on June 1, 2026 at 2:51 am
     foreach ($myArray as $item){ 
    
        $css_colors = array('verde' => '#008000', 
                                'rojo' => '#ff0000',
                                'azul' => '#0000ff',
                                'blanco' => '#ffffff',
                                'negro' => '#000000',
                                'amarillo' => '#ffff00');
        foreach ( $css_colors as $colorname => $value) {
    
         if ($colorname == $item) { 
                display_color($value);
        } 
       }
    }  
    
    function display_color($color)
    {
        echo '<div style="width: 40px; height: 40px; background: '. $color . ';">&nbsp;</div>';
    }
    

    You can Try something like this you need to map the spanish valus to the right hex css values. I hope it helps! for a list of css color visit go here http://www.w3schools.com/cssref/css_colornames.asp

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.