Reputation: 15506
How to write the following in PHP:
IF current page's name is
pagex.php
THEN please load these additional CSS rules:
#DIVS { color:#FFF }
IF current page's name isanotherpage.php
THEN please load following CSS rules:
#DIVS { color: #000 }
Upvotes: 1
Views: 3073
Reputation: 47966
PHP has some "magic constants" that you can inspect to get this information. Take a look at the ` __FILE__ constant.
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
So you can take this __FILE__
variable and execute the basename()
function on it to get the file name. The basename()
function returns the trailing name component of a path. Then you simply do a switch case to match the desired value -
$fileName = basename(__FILE__);
switch($fileName){
case 'pagex.php':
echo '<link .... src="some_stylesheet_file.css" />';
break;
case 'anotherpage.php':
echo '<link .... src="another_stylesheet_file.css" />';
break;
}
Your additional CSS rules can sit within those separate files.
Alternatively, if you don't want to split your css into multiple files, you can echo those specific rules into your page's head element like this -
echo '<style type="text/css">';
$fileName = basename(__FILE__);
switch($fileName){
case 'pagex.php':
echo '#DIVS { color:#FFF }';
break;
case 'anotherpage.php':
echo '#DIVS { color: #000 }';
break;
}
echo '</style>';
References -
Upvotes: 2
Reputation: 628
you can use is_page() function of wordpress in a customize manner as it is working on regular php.code is:
<?php
$baseurl = 'http://www.example.com'; //set the base url of the site
$mypage1 = $baseurl."/pagex.php"; //add the rest of the url
$mypage2 = $baseurl."/anotherpage.php"; //add the rest of the url
$currentPage = $baseurl.$_SERVER['REQUEST_URI'];// this gets the current page url
if($currentPage==$mypage1) {
//do something with you style or whatever..
}
else if($currentPage==$mypage2)
{
//do something with you style or whatever..
}
?>
you have to change it according to your needs. i think it will help you. happy coding!
Upvotes: 2
Reputation: 1258
You can just add in HTML head part one PHP if...else to load additional stylesheet according to page name.
<head>
<?php
if (basename(__FILE__) == 'one.php')
echo '<link .... src="style1.css" />';
elseif (basename(__FILE__) == 'two.php')
echo '<link ..... src="style2.css" />';
?>
</head>
Upvotes: 2
Reputation: 4331
like this:
<?php
if (basename(__FILE__) == 'pagex.php') {
echo '#DIVS { color:#FFF }';
} else if (basename(__FILE__) == 'anotherpage.php') {
echo '#DIVS { color:#000 }';
}
?>
Upvotes: 3