Tristan
Tristan

Reputation: 215

How do I assign a HTML title to different pages?

I am developing a website with oure PHP and HTML on an Apache server. No CMS.

All of my website's pages from the main directory and its subdirectories call one header.php file.

I have now run into an issue with all of the page <title>'s, as these are obviously defined in the header, so now I don't know how to set a different title for each page.

Can anyone help?

Upvotes: 1

Views: 875

Answers (2)

Chris Evans
Chris Evans

Reputation: 1265

The simplest way to do this is to have a variable that you set on the main page, before you include the header.php file, and then in the header.php file, use that title.

page.php

<?php

$title = "My Title";
include "header.php";

header.php

<title>

    // Default Title will be set if you forget to set the $title variable in page.php
    <?php echo isset($title) ? $title : "Default Title"; ?>

</title>

Upvotes: 3

durbnpoisn
durbnpoisn

Reputation: 4669

What I usually do is have the title tag like this:

<title><? echo $titleIs ?></title>

Then, on each page, before the header include, set the title:

<? $titleIs='Page Title' ?>

Upvotes: 1

Related Questions