Reputation: 343
I have my wordpress theme setting for different stylesheet selection which is set using if else statement at front end.
my wordpress setting may have one value from following pool of values
red ,green, blue, yellow, white, pink, black, grey ,silver or purple
My template :
<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; ?>
In this way I can get particular stylesheet as per need. But this php code become lengthy if there are more value in the option pool. So is there any way to shorten this using an array?
Upvotes: 0
Views: 450
Reputation: 49
<?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; ?>
You can fill the variable with $my_styles with all the available styles, be it from database or whatever..
Upvotes: 0
Reputation: 47996
There is no real need to use an array here. You are making changes to what CSS file you are including according to a certain value.
I think what you are looking for is a switch case command. Here is a simply example of what you may be able to do with it -
<?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;
}
?>
Using this method, you can include more than one change for each my_style
option. Note the use of a default case to handle any unexpected values...
Reference -
Upvotes: 0
Reputation: 9447
May be you can reduce it to
<link href="<?php bloginfo("template_url"); ?>/css/<?php echo get_option('my_style'); ?>.css" rel="stylesheet" media="all" />
I don't think you need an array if the function get_option
returns the string same as name of the css file.
Upvotes: 3
Reputation: 4157
This option:
<?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" />
Upvotes: 1