Simplifying if else statement using an array - php

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?

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.

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" />

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 -
switch structures

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

Related

Change default style based on title

I am trying to change the default style based on title of the page.I found that as the easiest way to accomplish needs.
I load the default styling option in header.php file:
<link rel="stylesheet" data-them="" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="all" />
I suppose <?php bloginfo('stylesheet_url'); ?> loads style.css which is on same path as header.php
I commented out this the line <link rel="stylesheet" data-them="" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="all" /> and tried to check title with php, based on that condition I wanted to load different style.css file.
<?php
$title = get_the_title();
if($title === "Title1"){
?><link rel="stylesheet" data-them="" href="style.css" type="text/css" media="all" /><?php
}
elseif($title === "Title2"){
?><link rel="stylesheet" data-them="" href="style2.css" type="text/css" media="all" /><?php
}
else{
?><link rel="stylesheet" data-them="" href="style3.css" type="text/css" media="all" /><?php
}
?>
This code does not work for me and not sure why.. Styling is messed up
Please find below style in your theme. Mostly enqueue in functions.php file.
EX:
wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(),
wp_get_theme()->get( 'Version' ) );
Please add below line after your theme's default style.
(get_stylesheet_uri())
wp_dequeue_style('twentynineteen-style');
After that check it your issue.
Please dequeue default style using below function.
wp_dequeue_style('default-style');

Change style.css by changing language in php

Does anyone know how to use condition in CSS?
I'm using PHP Script The thing is that I have 4 files with these names "style.css","style-rtl.css","bootstrap.min.css","bootstrap-rtl.min.css" that sometimes may be in RTL language.
So I want some sort of PHP code or any other solution that can detect if the language is Ar and set the direction to RTL with style-rtl.css & bootstrap-rtl.min.css , else language is En set direction to LTR and use style.css & bootstrap.min.css .
Here is some header.php code:
<!-- Bootstrap core CSS -->
<link href="<?php echo $this->config["url"] ?>/static/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo $this->config["url"] ?>/static/css/bootstrap-rtl.min.css" rel="stylesheet">
<!-- Component CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/themes/<?php echo $this->config["theme"] ?>/style.css">
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/themes/<?php echo $this->config["theme"] ?>/style-rtl.css">
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/static/css/components.min.css">
i solved this problem with :
<?PHP
if(isset($_SESSION['lang']))
{
switch($_SESSION['lang']):
case 'eng':
// LTR Style
?>
<link href="<?php echo $this->config["url"] ?>/static/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/themes/<?php echo $this->config["theme"] ?>/style.css">
<?PHP
break;
case 'ar':
// RTL Style
?>
<link href="<?php echo $this->config["url"] ?>/static/css/bootstrap-rtl.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/themes/<?php echo $this->config["theme"] ?>/style-rtl.css">
<?PHP
break;
default:
?>
<link href="<?php echo $this->config["url"] ?>/static/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<?php echo $this->config["url"] ?>/themes/<?php echo $this->config["theme"] ?>/style.css">
<?PHP
endswitch;
}
?>

open cart 2.2 Change style.css based on language or direction

I am trying change style.css based on language or direction in open cart 2.2
I have this input:
<link type="text/css" href="view/stylesheet/stylesheet.css" rel="stylesheet" media="screen" />
i want change stylesheet.css to stylesheet-rtl.css
This is my try:
<?php $session = new Session();?>
<?php $lang = $session->data['language']; ?>
<?php if ($lang == 'ar') { ?>
<link type="text/css" href="view/stylesheet/stylesheet-rtl.css" rel="stylesheet" media="screen" />
<?php } else { ?>
<link type="text/css" href="view/stylesheet/stylesheet.css" rel="stylesheet" media="screen" />
<?php } ?>
This worked for opencart 2.0.X but not in opencart 2.2.
What changed?
if you are using OC 2.2 you can try this code
<?php $direction = $this->language->get('direction'); ?>
<?php if ($direction == 'rtl') { ?>
<link type="text/css" href="view/stylesheet/stylesheet-rtl.css" rel="stylesheet" media="screen" />
<?php } else { ?>
<link type="text/css" href="view/stylesheet/stylesheet.css" rel="stylesheet" media="screen" />
<?php } ?>

Wordpress: Special css for TWO Pages

In my blog i have two sites, that need a special css. For the site "projects" i did this:
<?php if ( is_page('projects') ) { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/projects.css" type="text/css" media="screen" />
<?php } else { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>
This works like a charm.. but i also need a special css for the home..
How can i do that..? if i just put:
<?php if( is_home() ) { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/home.css" type="text/css" media="screen" />
<?php } ?>
Above the other code, it will not work..
The whole code i use now is:
<?php if( is_home() ) { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/home.css" type="text/css" media="screen" />
<?php } ?>
<?php if ( is_page('projects') ) { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/projects.css" type="text/css" media="screen" />
<?php } else { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>
Thanks for your help - and sorry for my english !
You should do:
<?php if(is_front_page()) { ?>
// html code here
<?php } else if ( is_page('projects')) { ?>
// html code here
<?php } else { ?>
// html code here
<?php } ?>

PHP How right replace element in page?

Good day.
We have test.php with next code:
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{Text}</p>
</div>
<!-- STYLE_TEST1 and STYLE_TEST2 - set in define as "/css/styletest1.css" and "/css/styletest2.css" -->
and index.php with code:
$content = file_get_contents("test.php");
$content = ereg_replace("{Text}", 'hello', $content);
echo $content;
in result we get code:
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{Text}</p>
</div>
we have problem with styles, becose echo $content; print <?php echo STYLE_TEST1; ?> and <?php echo STYLE_TEST2; ?> and not replace on values which set in define for this elements(not change on "/css/styletest1.css" and "/css/styletest2.css").
Tell me please how right make replace?
P.S.: we need that styles was include in file test.php, ie file test.php should have lines:
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
Thanks all for help!
This is the best decision for me on my question
function getTemplate($file) {
ob_start(); // start output buffer
include $file;
$template = ob_get_contents(); // get contents of buffer
ob_end_clean();
return $template;
}
$content = getTemplate("test.php");
$content = ereg_replace("{Text}", 'hello', $content);
echo $content;
A way to fix this is to make it a variable....
just make sure you have
define("STYLE_TEST1", "/css/styletest1.css");
define("STYLE_TEST2", "/css/styletest2.css");
test.php
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST1; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo STYLE_TEST2; ?>" />
<div class="container">
<p class="test">{<?=$variable?>}</p>
</div>
<!-- STYLE_TEST1 and STYLE_TEST2 - set in define as "/css/styletest1.css" and "/css/styletest2.css" -->
index.php
$variable = 'hello';
include 'test.php';
The result will be:
<link rel="stylesheet" type="text/css" href="/css/styletest1.css" />
<link rel="stylesheet" type="text/css" href="/css/styletest2.css" />
<div class="container">
<p class="test">{hello}</p>
</div>

Categories