rickky
rickky

Reputation: 19

if is_page function not working for my PHP website

I am using if else conditional tag in my PHP website. My code is:

<?php if(is_page('print-systems-overview.php')) { ?>
<style type="text/css">
.activeNew{background:#4E4E4E;}
</style>
<?php } elseif (is_page('software.php')) { ?>
<style type="text/css">
.activeNew{background:grey;}
</style>
<?php } ?>

But I got fatal error: Fatal error: Call to undefined function is_page() in D:\xampp\htdocs\project1234\header.php on line 36

After research on it I come to know that the is_page is working on wordpress only it is wordpress function and not working on core PHP. Is there any way to do this type of function in core PHP.

Please help me to fix this. I am new in PHP so not much knowledge about it. Thank

Rickky

Upvotes: 1

Views: 1469

Answers (2)

Vidya L
Vidya L

Reputation: 2314

You can use basename($_SERVER['PHP_SELF']); will return the current filename

function is_page(){
   $file_name = basename($_SERVER['PHP_SELF']);
   if($file_name == 'print-systems-overview.php') { ?>
     <style type="text/css">
    .activeNew{background:#4E4E4E;}
     </style>                
   <?php } elseif ($file_name == 'software.php') { ?>
     <style type="text/css">
     .activeNew{background:grey;}
     </style>
   <?php }
 } ?>

just call the is_page() function

Upvotes: 1

Saqueib
Saqueib

Reputation: 3520

Is page is wordpress function, if you wanted to create something like this in core php here is a way to do

Create a file to hold this function like functions.php so you can reuse it and include it in page

functions.php

<?php

function is_page($page_name) {
   $url = $_SERVER['SCRIPT_NAME'];
   //Check for current page equal to $page_name
   return basename($url) == $page_name;
}

and then check the page call it like

<?php include('functions.php') ?>

<?php if(is_page('print-systems-overview.php')) { ?>
<style type="text/css">
.activeNew{background:#4E4E4E;}
</style>
<?php } elseif (is_page('software.php')) { ?>
<style type="text/css">
.activeNew{background:grey;}
</style>
<?php } ?>

Upvotes: 1

Related Questions