2013-02-19 122 views
0

我有我的wordpress主題設置爲不同的樣式表選擇,這是使用if else語句在前端設置的。簡化if else語句使用數組

我的wordpress設置可能有一個值從值

red ,green, blue, yellow, white, pink, black, grey ,silver or purple 

的以下池我的模板:

<link href="<?php bloginfo("template_url"); ?>/style.css" rel="stylesheet" media="all" /> 

<?php if (get_option('my_style') == "red" : ?> 
<link href="<?php bloginfo("template_url"); ?>/css/red.css" rel="stylesheet" media="all" /> 
<?php endif; ?> 

<?php if (get_option('my_style') == "green" : ?> 
<link href="<?php bloginfo("template_url"); ?>/css/green.css" rel="stylesheet" media="all" /> 
<?php endif; ?> 

<?php if (get_option('my_style') == "blue" : ?> 
<link href="<?php bloginfo("template_url"); ?>/css/blue.css" rel="stylesheet" media="all" /> 
<?php endif; ?> 

<?php if (get_option('my_style') == "yellow" : ?> 
<link href="<?php bloginfo("template_url"); ?>/css/yellow.css" rel="stylesheet" media="all" /> 
<?php endif; ?> 
. 
. 
. 
. 
. 
<?php if (get_option('my_style') == "purple" : ?> 
<link href="<?php bloginfo("template_url"); ?>/css/purple.css" rel="stylesheet" media="all" /> 
<?php endif; ?> 

這樣我可以得到特別的樣式表按需要。但是如果在選項池中有更多的價值,這個php代碼就會變得冗長。那麼有什麼辦法可以使用數組來縮短它嗎?

回答

1

此選項:

<?php 
$arraystyle=array("red", "green", "blue", "yellow", "white", "pink", "black", "grey", "silver", "purple"); 

$val=get_option('my_style'); 
if(!in_array($val, $arraystyle)){ 
    echo "Style not found"; 
    return false; 
} 
?> 

<link href="<?php bloginfo("template_url"); ?>/css/<?php echo $arraystyle[$val];?>.css" rel="stylesheet" media="all" /> 
+0

Thnkas lobo1我正在檢查 – galexy 2013-02-19 15:01:29

+0

'返回false;'語句沒有真正有意義的沒有exp合法地進入功能或包含文件。如果你把它放在主腳本頁面上,那麼它後面什麼都不會執行,而不是OP在初始文章中的內容,這將是一個沒有風格但完整的頁面。 – 2013-02-19 15:09:00

+0

@PatrickM多數民衆贊成在右邊,這只是一個示例實現 – Lobo 2013-02-19 15:11:58

3

可能是你可以把它降低到

<link href="<?php bloginfo("template_url"); ?>/css/<?php echo get_option('my_style'); ?>.css" rel="stylesheet" media="all" /> 

我不認爲你需要一個數組,如果get_option返回字符串相同的CSS的名稱的功能文件。

+0

我認爲你是最簡單的一個,但我想看看如何在其中使用數組 – galexy 2013-02-19 15:00:48

+1

@galexy然後轉到Lobo的方法 – 2013-02-19 15:01:54

+0

如果'get_option'根據用戶輸入繪製,那麼你是正確的檢查它對陣的有效選項@ galexy。但是你可以將任何這樣的檢查移動到get_option函數本身,如果它在你的控制之下(不是一些wordpress的東西),它不是更通用的目的。 – 2013-02-19 15:11:06

0

這裏沒有真正需要使用數組。您正根據某個值對包含的CSS文件進行更改。

我認爲你要找的是一個switch case命令。這裏是你可以用它做一個簡單的例子 -

<?php 

$my_style = get_option('my_style'); 
switch($my_style){ 
case "red": 
    echo '<link href="'. bloginfo("template_url"). '/css/red.css" rel="stylesheet" media="all" />'; 
break; 
case "green": 
    echo '<link href="'. bloginfo("template_url"). '/css/green.css" rel="stylesheet" media="all" />'; 
break; 
default : 
    echo '<link href="'. bloginfo("template_url"). '/css/default.css" rel="stylesheet" media="all" />'; 
break; 
} 

?> 

使用這種方法,您可以包括每個my_style選擇一個以上的變化。注意使用默認的情況下處理任何突發值...

參考 -

+1

我想他在問題中指定'有沒有什麼方法可以使用數組來縮短這個問題呢? – 2013-02-19 15:03:17

+0

Thanks Lix!對於這個開關,但它也變得冗長 – galexy 2013-02-19 15:03:43

+0

@gal - 更短!==更好。我會去可讀性... – Lix 2013-02-19 15:04:17

0
<?php 
$my_styles = array(
    'red', 
    'green', 
    'blue', 
    'yellow', 
    'white', 
    'pink', 
    'black', 
    'grey', 
    'silver' 
); 
?> 
<?php if(in_array($my_style = get_option('my_style'),$my_styles)) : ?> 
    <link href="<?php echo bloginfo("template_url")."/css/{$my_style}.css"; ?>" rel="stylesheet" media="all" /> 
<?php endif; ?> 

您可以填寫與$ my_styles變量的所有可用的樣式,無論是從數據庫或其他什麼..

+0

這是非常Салман答案,檢查,以確保什麼樣的時髦不會跟風格。因爲它可能來自用戶輸入並可能與網站的其他用戶共享...並確保實際上有一種選擇的樣式... – holycowbatman 2013-02-19 15:24:19