1

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

1 Answer 1

3

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; }"; 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.