1

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 is anotherpage.php
THEN please load following CSS rules:
#DIVS { color: #000 }

2
  • I dont know where to start, and which ways there are to test a page's name with or without extensions. Perhaps there are multiple ways and I think its valuable for many designers to see some possibilities in such a scenario. Commented Aug 24, 2012 at 7:35
  • change the CSS class of the divs based on the page and create two CSS rules. Commented Aug 24, 2012 at 7:38

4 Answers 4

3

like this:

<?php
   if (basename(__FILE__) == 'pagex.php') {
      echo '#DIVS { color:#FFF }';
   } else if (basename(__FILE__) == 'anotherpage.php') {
      echo '#DIVS { color:#000 }';
   }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

You should mention that these echo commands need to output into the right place on the page - in the <head> element within a <style> tag.
2

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 -

3 Comments

Thanks alot @Lix , One question: when I use your proposal as an include (rules.php) and import the include inside a say index.php then the filename turnsout to be rules.php, instead of index.php... how to solve that? As I want to include the special rules in various webpages.
You are very welcome! Perhaps you could wrap these statements in a function and pass as a parameter the original value of __FILE__...
yes solved! Thanks again for your very extensive and thorrrrough answer :)
2

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>

Comments

2

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.