Conditional CSS based on boolean PHP variable - php

How can I have certain CSS load only if a specific PHP variable is true and have other CSS load of the variable is false?

Have it in 2 separate files and have the correct file load depending on the boolean state or use if statements to echo the code you want within the document depending on the boolean state.
<style type='text/css'>
<?php
if(boolean)
{
echo "elementName { attribute: value; } ";
echo "elementName2 { attribute: value;} ";
}
else
{
//echo css here
}
?>
</style>
or, in the head tag:
<?php
if (boolean)
{
echo "<link rel='stylesheet' type='text/css' href='1.css'>";
}
else
{
echo "<link rel='stylesheet' type='text/css' href='2.css'>";
}
?>
Also, form vs content; it would be more practical to keep your CSS in a separate file and have a particular CSS script load depending on a variable rather than mix up the CSS with the HTML.

<link rel="stylesheet" type="text/css" href="<?php echo ($var) ? "css1" : "css2"; ?>.css">

In your HTML page, on the <head> section, you can write some simple conditions in PHP to include or not a CSS:
<?php
if ($yourCondition)
{
?>
<link rel="stylesheet" type="text/css" href="http://path/to/css1.css" />
<?php
}
else
{
?>
<link rel="stylesheet" type="text/css" href="http://path/to/css2.css" />
<?php
}
Note: Avoid using inner style, because all style should be in CSS (presentation layer separated from the HTML which is a structure and data layer)

Related

If else statement for condition contains specific string only

I want to load my style-sheet's and script's inside wordpress header to show documentation in my front-page/homepage for specific condition supply by wordpress option setting get_option('my_main_scripts').
Pool of values for this option setting are javascript library, javascript yui, javascript jquery, javascript dojo and so on. All option value consist with common word javascript.
and want to load css if any one value is selected from the pool which contains word javascript.
So writing condition with each option using OR (||) , I think its better to grab the common word from the value and write down the condition .here is javascript how can I get this?
<?php if ( is_home() || is_front_page() ) { ?>
<?php if (get_option('my_main_scripts') == "javascript library"): ?>
<link rel="stylesheet" href="<?php bloginfo("template_url"); ?>/css/javadoc.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo("template_url"); ?>/js/javadoc.dojo.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo("template_url"); ?>/js/javadoc.yui.css" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo("template_url"); ?>/js/javadoc.jquery.css" type="text/css" media="screen" />
<?php get_template_part('documation'); ?>
<?php endif; ?>
<?php } ?>
Use strpos.
if(strpos(get_option('my_main_scripts'),'javascript') !== false )
Also have a look at preg_match.
if(preg_match('/javascript/',get_option('my_main_scripts')))

How to load dynamic CSS files based on condition

I have a index.php file which loads (require) 2 different files based on a condition.
Visiting this link will cause;
mysite.com/index.php will load stats.php (require("stats.php");)
Visiting this link will cause;
mysite.com/index.php?auth="jgbsbsbasm" will load encry.php (require("stats_encry.php");)
Done with this code:
<?php
if(isset($_GET['auth'])) require("stats.php");
else require("stats_encry.php");
?>
This works fine. Now my question is; I have a CSS file in the header as static;
<link rel="stylesheet" href="cv.css">
What I want is now to load cv.css file for stats.php and cv1.css for stats_encry.php respectively.
How do I do this?
In your header replace
<link rel="stylesheet" href="cv.css">
with
<?php if(isset($_GET['auth'])){ ?>
<link rel="stylesheet" href="cv.css">
<?php } else { ?>
<link rel="stylesheet" href="cv1.css">
<?php } ?>
You can use this like below syntax
<?php
if(isset($_GET['auth'])){
echo '<link rel="stylesheet" href="cv.css">';
} else {
echo '<link rel="stylesheet" href="cv1.css">';
}
?>

Switching links to css via PHP

My idea is to switch multiple links to CSS files, if special URL was detected. But I have a trouble: my code includes only css in the first if..else statement. And it doesn't depend on the URL.
Here is my code.
http://pastebin.com/Jm3QFDmH
ported from pastebin
<?php
// get first folder in URL
$f_folder = substr(substr($_SERVER["REQUEST_URI"],1), 0, strpos(substr($_SERVER["REQUEST_URI"],1), "/"));
//get full directory structure from URL for current page
$full_path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//css for account
if ($f_folder='account') {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/mainstyle/account.css" type="text/css" charset="utf-8" />
<?php
} elseif ($f_folder='signin'||$f_folder='signup'||$full_path='/account/resetPassword'||$full_path='/account/logout') {
?>
<link rel="stylesheet" href="/mainstyle/login-signup.css" type="text/css" charset="utf-8" />
<?php
} else {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<?php
}
?>
Where is mistake?
if and elseif, should have == comparison operator , not = assignment
Line 7 should be
if ($f_folder=='account') {

How to display multiple single page templates based on categories for WordPress?

I am using multiple stylesheets and need the pages to differ based on category.
I added the following to my header.php, but shows base theme`s single entry template. Any ideas?
<?php if (is_category('20')) { ?>
<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />
<?php } else {?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/style.css" />
<?php } ?>
I am using the same theme as these sites, but I use two themes on my site compared to these.
http://marioortega.net/
http://atelier6.co.uk/
When you click one of the thumbnails, the single ends up on top and the thumbnails at the bottom.
All:
Thank you for looking into this. In order for me to get this to work, the problem was actually multiple single.php files. This can be solved by having your single.php look like this,
<?php
$post = $wp_query->post;
if ( in_category('20') ) {
include(TEMPLATEPATH . '/single1.php');
} else {
include(TEMPLATEPATH . '/single2.php');
}
?>
I have also edited the question for other people looking for this answer.
Shouldn't you be using echo statement to put the link into your page.
<?php
if (is_category('20')) {
echo '<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />';
}
else {
echo '<link rel="stylesheet" type="text/css" href="'.bloginfo('template_url').'/style.css" />';
}
?>

PHP stylesheet print switcher problem revisited?

Okay I have this style sheet switcher which will only works if I leave out the media="print" from the style sheet link.
I was wondering how can I fix this problem without leaving out the media="print" attribute.
Here is the PHP code.
<!-- Print Script -->
<?php if (isset($_GET['css']) && $_GET['css'] == 'print') { ?>
<meta name="robots" content="noindex" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<script type="text/javascript">
//<![CDATA[
if(window.print())
onload = window.print();
else
onload = window.print;
//]]>
</script>
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<?php } ?>
<!-- End Print Script -->
And here is the link you click to change the style sheet.
Print This Page
You don't need PHP code for determining whether to output the CSS file for print. By default the browser won't render the "print" stylesheet and should ignore the "screen" stylesheet when printing.
There may be some rendering issues: maybe the browser hasn't had enough time to render the page and feed it correctly to the printer.
A simplified solution would be:
<head>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<script type="text/javascript">
function print_it() {
if(window.print())
onload = window.print();
else
onload = window.print;
}
</script>
</head>
<body>
Print This Page
</body>

Categories