考慮一個下面的函數與一個選項呼應了:正確地回顯一堆使用PHP的CSS選項?
function dynamic_options() {
$getheadercolor = get_header_textcolor();
$options_social = get_option('sandbox_theme_social_options');
$wrapper_background_color = get_option('wrapper_background_color');
if($getheadercolor !='blank'){
echo '<style type="text/css">';
}
if($getheadercolor !='blank') {
echo "\n"."#header a{
color:#$getheadercolor;
}";
}//End If $getheadercolor
if($getheadercolor !='blank'){
echo "\n".'</style>';
}
}// End Dynamic options
它輸出這樣的事情在我的頭(它完美的作品,準確地做,因爲我想)
<style type="text/css">
#header a{
color:#30409b;
}
</style>
現在這裏是問題:這個功能不會有隻有一個選項,但一堆選項(20-30選項)。所以爲了說明我的觀點,讓我們說現在我的功能將有五個選項。因此,它看起來就像這樣:
function dynamic_options() {
$getheadercolor = get_header_textcolor();
$options_social = get_option('sandbox_theme_social_options');
$wrapper_background_color = get_option('wrapper_background_color');
//My main "problem" is an IF Statement below because it will look like a mess
//With 20 options or more and with all those OR inside it...
if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !=''
|| $backgroundcolor !='' || $menucolor !=''){
echo '<style type="text/css">';
}
if($getheadercolor !='blank') {
echo "\n"."#header a{
color:#$getheadercolor;
}";
}//End If $getheadercolor
if($sitecolor !='blank') {
echo "\n"."#wrapper{
background-color:#$sitecolor;
}";
}//End If $sitecolor
if($textcolor !='blank') {
echo "\n".".entry p{
color:#$textcolor;
}";
}//End If $textcolor
if($backgroundcolor !='blank') {
echo "\n"."body{
background-color:#$backgroundcolor;
}";
}//End If $backgroundcolor
if($menucolor !='blank') {
echo "\n".".nav{
background-color:#$menucolor;
}";
}//End If $menucolor
//So to even close my style tag i need a bunch of those statments
if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !=''
|| $backgroundcolor !='' || $menucolor !=''){
echo "\n".'</style>';
}
所以我上面的功能將工作但這部分if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !='' || $backgroundcolor !='' || $menucolor !='')
似乎只是我錯了。
因爲這個IF語句將有多於選項,所以我恐怕我的代碼會變得緩慢和低效。
我的PHP力不強......所以我唯一的(不理想)的解決方案是簡單地忽略這兩個IF語句,像這樣:
function dynamic_options() {
$getheadercolor = get_header_textcolor();
$options_social = get_option('sandbox_theme_social_options');
$wrapper_background_color = get_option('wrapper_background_color');
echo '<style type="text/css">';
if($getheadercolor !='blank') {
echo "\n"."#header a{
color:#$getheadercolor;
}";
}//End If $getheadercolor
if($sitecolor !='blank') {
echo "\n"."#wrapper{
background-color:#$sitecolor;
}";
}//End If $sitecolor
if($textcolor !='blank') {
echo "\n".".entry p{
color:#$textcolor;
}";
}//End If $textcolor
if($backgroundcolor !='blank') {
echo "\n"."body{
background-color:#$backgroundcolor;
}";
}//End If $backgroundcolor
if($menucolor !='blank') {
echo "\n".".nav{
background-color:#$menucolor;
}";
}//End If $menucolor
echo "\n".'</style>';
}// End Dynamic options
現在我的代碼沒有那些IF語句將工作太但現在的問題是,如果沒有選項,我的函數仍然會在我的頭文件中回顯一個空的 CSS樣式標記:<style type="text/css"></style>
,這是我不想要的。
有人可以給我一個例子或建議,使這個功能更好地工作?
P.S.我正在考慮自己作爲PHP noob所以如果有人可以給我一個很好和明確的建議或例子,它將不勝感激!謝謝!!!
使用[less](http://lesscss.org/)和[php](http://leafo.net/lessphp/)? – Prasanth