如果有一个管理区域,用户可以在其中选择类别名称和相关颜色以匹配它。有10个选项(即10个类别,10种颜色)
然后输出到标题以控制类别颜色:
例如,
$cat1 = get_option('catname1');
$col1 = get_option('col1');
$cat2 = get_option('catname2');
$col2 = get_option('col2');
等等直到10.然后按如下方式输出到CSS(如果用户在管理面板上输入了任何内容):
if($cat1){echo "
.".$cat1"{ color:".$col1." !important; }
.".$cat1." { background-color:".$col1." !important; }" };
我如何在foreach中组合这些语句(基本上是从cat1到cat10)?
答案 0 :(得分:3)
您可以使用for
循环:
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; }";
}
}