I have a site that has a Header file that is included on every page. It has the page title and description, and menu for the page. I want to be able to have it change for a particular page/file. Like if i had tech-reviews.php, the title would be "Tech Reviews" and on the help.php file the title would be "Help" I am guessing the easiest way to do this would be with an If Else statement?
You can add this code in your header file. <?php //add all your file and their title in this array $pages = array('Index.php'=>'Home page', 'Help.php'=>'Help', 'Contact.php'=>'Contact Us' ); foreach($pages as $url => $label){ if(strpos($_SERVER['PHP_SELF'],$url) > 0){ $title = $label; break; }//end if }//end foreach ?> <html> <head> <title> <?php echo $title; ?> </title> </head> PHP:
<?php $filename = basename($_SERVER['PHP_SELF']); if ($filename == 'tech-reviews.php') { $title = 'Tech Reviews'; } elseif ($filename == 'help.php') { $title = 'Help'; } Etc... ?> PHP: And then just echo $title wherever you want the title to be.
Try this code: $t= $_SERVER[SCRIPT_NAME]; $t= str_replace('.php','',$t); $t= preg_replace('/[^A-Za-z0-9]/', ' ', $t); //$t has your title based on file name // you can also use $_SERVER[PHP_SELF]
In each of your file "tech-reviews.php" , "help.php ".... like in "tech-reviews.php" file add this code: $title="Tech Reviews"; and in "help.php" file add: $title="Help"; after customizing your every page, lets come to HEADER FILE which is included in each of your page. customize your HEADER FILE as for example: <html> <head> <title><?php echo $title; ?> </head> </html> ----- It will print text which is currently present in the $title Variable, for example during HELP.php file the $title variable will contain the text "HELP" and during the tech-reviews.php file, the $title variable should have "Tech Reviews" in its variable. Thanks.