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