Want different page titles for your PHP-based site, but still want to use a header.php
include? No problem.
header.php
In your header.php
file, change <title>Same Title Everywhere</title>
to the following:
<?php if (isset($title)) { echo$title . " | Site Name"; } else { echo"Site Name"; } ?>
Change Site Name
to the name of your site.
For example, my trading card post <title>
tag looks like this:
<?php if (isset($title)) { echo$title . " | portia's post"; } else { echo "portia's post"; } ?>
What it means/does
if (isset($title)) { echo$title . " | Site Name"; }
If you’ve defined $title
in your other pages, it will display with a |
as a separator, plus your site name. e.g. About | Autistic Jane
.
else { echo "Site Name"; }
If not, it will display your site name as a fallback, e.g. Autistic Jane
.
Every single page
Before you call header.php
, you need to define $title
.
<?php $title = 'Page Title'; include('header.php'); ?>
e.g.
<?php $title = 'About'; include('header.php'); ?>
If you’re using a multi-page PHP file, e.g.
<?php if (!$_SERVER['QUERY_STRING']) { //first page content that only stays on first page ?> page stuff <?php } elseif ($_SERVER['QUERY_STRING'] == "collecting") { // another page ?> oh another page <?php } // close everything ?>
Then it will need to look like this:
<?php if (!$_SERVER['QUERY_STRING']) { // main page $title = 'Page Title'; include('header.php'); ?> yada yada <?php } elseif ($_SERVER['QUERY_STRING'] == "page") { // another page $title = 'Another Page Title'; include('header.php'); ?>
There was another way I used to do this that didn’t require $title
to be defined first, but I can’t find it at the moment.
I’ll update this post if I do.
Simple post for simple TCG trading post tutorial.
Love this post?
Support me by subscribing to my blog and/or buying me a cuppa:
Leave a comment