简化if else语句使用数组

问题描述:

我有我的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代码就会变得冗长。那么有什么办法可以使用数组来缩短它吗?

此选项:

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

可能是你可以把它降低到

<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

这里没有真正需要使用数组。您正根据某个值对包含的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

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