I’ve had scenarios pop up multiple times where I wanted to 1) custom code titles for my webpages based on the name of the webpage 2) be able to change them from one page instead of having to comb through multiple webpages to change each page individually. I’m including a rewrite of the code I’ve written before to be easily modified and used by you!
// A script to specify the title on each webpage based on which page the user is on // Pull the name of the current page and assign it to a variable $currentpage = basename($_SERVER['PHP_SELF']); // Now assign the title you want based on which page it is // You can change what I've included below to your own pages & titles if($currentpage == 'currentpage.php') { echo "<title>Kimenders.com > Current Page Script</title>"; } elseif ($currentpage == 'anotherpage.php') { echo "<title>Kimenders.com > Just Some Other Page</title>"; } // To add a new page to check for, uncomment the code below and update it with the page name and the title you want to use // You can copy and paste the code below an unlimited amount of times for how many ever pages you want to add // elseif ($currentpage == 'nameofyourpage.php') { // echo "<title>The title you want to use</title>"; // }
Implementing the Code Fully
1) Save the code into it’s own file. For the purpose of these instructions, we’ll call the file currentpage.php.
2) Upload it to the directory where you’ll be using it.
3) In any php page you want to use it, make sure to call the file, like so:
include ("currentpage.php");
Hi, I see now. Yes. I agree on non-spacing issue. But can’t php capitalize the first letter of the file?
Anyways, thanks for the proper explanation. Looking at your other code now. 😀
PHP can capitalize and lowercase the first letter in a string. You would use ucwords for capitalizing. For example: $title = ucwords($currentpage); From there, you would just use $title as the page title variable in your header.
Question. Isn’t there a way to just look at the php file itself and take it’s name and omitting the URL and file extension? It would save a lot of hassle of adding extra lines.
Kind of like how WordPress will create links based on the name of the page? If that’s what you’re talking about, I’m sure there is. I came up with a quick and not terribly extensive solution to what you asked about. You can check it out here. This solution, however, is lacking as it doesn’t capitalize the words from the file name nor does it add spaces. For example, thisawesomepage.php would output thisawesomepage as the title which totally lacks formatting. I’m sure it can be done but it’s not something I have the time to work on.
I prefer to do it the original way for a handful of reasons. I prefer to keep my page file names short and some times, my page titles can be a bit long. Doing it this way also makes it to where you are not pigeon-holed into using just whatever page file name you have. For example, I’d much rather be able to assign a title like this ‘Frequently Asked Questions: Hopefully This Helps’ rather than something like ‘faq’ or ‘frequentlyaskedquestions’. Hopefully that makes sense. I don’t mind the extra effort it takes to add custom titles.