mumford1 — 2012-08-27T09:38:56-04:00 — #1
I have searched for help on automatically adding the page tile of my pages based on the page name itself.
Site Name | Page Name
So this page: www.sitename.com/about-us.php would return in the <title> tag:
My Site | About Us
I just cant find the right solution. Could somebody please help me on this?
cups — 2012-08-27T10:17:41-04:00 — #2
Use a combination of str_replace() and ucwords() functions to turn about-us.php into "About Us", but I dont see how you'd turn sitename.com to "My Site"
mumford1 — 2012-08-27T10:24:35-04:00 — #3
Thanks for the response, when I mentioned site name I meant for example "Sitepoint" or the name of the actual website.
I am a complete novice to PHP so I am really unsure how I would get this to work.
kduv — 2012-08-27T17:24:23-04:00 — #4
Try something like this:
$page_title = 'SiteName | ' . ucwords(str_replace(array('-', '/', '.php'), array(' '), $_SERVER['PHP_SELF']));
<title><?= $page_title ?></title>
<!-- whatever else you wanna put on your page -->
mumford1 — 2012-08-30T02:06:20-04:00 — #5
many Thanks for the reply.
I tried your code but it is outputting the following
<title>SiteName | Kingswoodprogrammesictsoundscape</title>
The reason being I think is because some of my pages are in directories for example "Programmes"
So I need it to be the following <title>SiteName | Programmes | ictsoundscape</title>
Any ideas as to how I would do this?
kduv — 2012-08-30T02:33:10-04:00 — #6
I gave you a good example. Figure out the rest.
cups — 2012-08-30T05:07:33-04:00 — #7
You had better post some example strings showing what they are at the moment vs what you want them to read, and maybe explain where are you getting the rest of the page content from and why you only want the title of the page to be dynamic. Why not the <h1></h1> heading too?
rvand85 — 2012-08-30T05:50:27-04:00 — #8
$input = str_replace('-', ' ',$input);
$pos = strpos($input,'.');
if($pos > 0)
$input = substr($input,0,$pos);
$input = ucwords($input);
$aTitle = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
$aTitle = array_reverse($aTitle);
echo implode(' | ',$aTitle).' | PUT SITENAME HERE';
For an url like: http://localhost/folder1/folder2/folder3/about-us.php it will output:
About Us | Folder3 | Folder2 | Folder1 | PUT SITENAME HERE
Maybe this can inspire you to create something nice.
blogaddition — 2012-09-19T06:10:13-04:00 — #9
Use The following code
$page = $SERVER['REQUESTURI'];
$pageName = str_replace('/','',$page);
$pageTitle = ucwords(str_replace('-',' ',$pageName));
<title><?php echo $pageTitle;?></title>
<!-- Place you body contetn here-->