up vote 1 down vote favorite
share [g+] share [fb]

If have an admin area where users can select a category name and an associated color to match it. There are 10 options (i.e. 10 categories, 10 colors)

This then gets outputted to the header to control category colors:

So for example,

 $cat1 = get_option('catname1');
 $col1 = get_option('col1');

 $cat2 = get_option('catname2');
 $col2 = get_option('col2');

and so on until 10. These are then outputted to CSS as follows (if the user has inputted anything on the admin panel):

if($cat1){echo "
.".$cat1"{ color:".$col1." !important; }
.".$cat1." { background-color:".$col1." !important; }" }; 

How would I combine these statements in a foreach (basically to go from cat1 to cat10)?

link|improve this question

feedback

1 Answer

up vote 2 down vote accepted

You can use a for loop:

for ($i=1; $i<11; $i++) {
    $cat = get_option('catname' . $i);
    $col = get_option('col' . $i);
    if ($cat) {
        echo ".$cat { color: $col !important; }
              .$cat { background-color: $col !important; }"; 
    }
}
link|improve this answer
Exactly what I was after, cheers :) – Martin Feb 7 at 19:21
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.