在我的模板上,我有一个字段,用户可以在其中选择和选择他们网站的背景。
这是从background-one
数组中的 ID 调用的。
我的问题是,如果只为用户提供以下选项,我如何能够正确地将其输出到我的 CSS 的 head 部分:
1) 仅选择背景颜色
背景:#990000;
2) 仅选择背景图像
背景:url(../images/example.jpg) 不重复左上滚动;
3)或背景颜色和图像的选择
背景:#990000 url(../images/example.jpg) 不重复左上滚动;
这是我当前将脚本输出到 css head 部分的代码片段:
<?php $background = of_get_option('background-one'); {
if ($background['color'] || $background['image']) {
echo 'body {
background: ' . $background['color'] . ' url(' . $background['image']. ') ' .$background['repeat']. ' ' .$background['position']. ' ' .$background['attachment']. ';';
}
else if ($background['color']) {
echo 'body {
background: ' . $background['color']. ';';
}
else if ($background['image']) {
echo 'body {
background: ' . 'url(' . $background['image']. ') ' .$background['repeat']. ' ' .$background['position']. ' ' .$background['attachment']. ';';
};
}
?>
但这样一来,结果是:
1) 仅选择背景颜色
背景:#990000 url() 不重复左上滚动;
2) 仅选择背景图像
背景:
url(../images/example.jpg) 不重复左上滚动;
3)或背景颜色和图像的选择
背景:#990000 url(../images/example.jpg) 不重复左上滚动;
由于我对编写 PHP 还很陌生,我将如何重新编写上面的代码片段以获得我想要的结果?