如何为不同的页面分配HTML标题?

时间:2016-06-16 20:51:50

标签: php html5 title

我正在Apache服务器上开发一个包含PHP和HTML的网站。没有CMS。

主目录及其子目录中的所有网站页面都会调用一个header.php文件。

我现在遇到了所有页面<title>的问题,因为这些显然是在标题中定义的,所以现在我不知道如何为每个页面设置不同的标题。 / p>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

最简单的方法是在包含header.php文件之前,在主页面上设置一个变量,然后在header.php文件中使用该标题。

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>

答案 1 :(得分:1)

我通常做的是这样的标题标签:

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

然后,在每个页面上,在标题包括之前,设置标题:

<? $titleIs='Page Title' ?>