WebDesign: Header file, but with custom Page titles?

12,814

Solution 1

Not knowing more about your file inclusion scheme, the simplest way would be:

page2.php

<?php
$pageTitle = 'Page 2';
include 'header.php';
?>

<div>My content</div>

<?php include 'footer.php'; ?>

header.php

<html>
    <head>
        <title> My Site : <?php echo $pageTitle ?> </title>
    </head>
<body>

footer.php

</body>
</html>

Solution 2

webbiedave's answer is perfectly fine, but in the long run, you should really learn to use either a decent template language (Smarty, Twig), or a PHP framework that has it's own templating. Kohana and Codeigniter are both pretty easy to get into.

Solution 3

If i were to add some code before including the header, will it help?

<?php
    $currentPage = "Random Page Title";
    include "header.php";
?>

And then use the value in header.php so print the page title?

Share:
12,814

Related videos on Youtube

n a
Author by

n a

Updated on April 20, 2022

Comments

  • n a
    n a about 2 years

    I've been using headers to create templates for websites. It's easy, and very convenient for debugging.

    I now face the problem of using head BUT with custom page titles. If this is my header.php >

    <html>
        <head>
            <title> My Site : ??? </html>
        </head>
    <body>
    
    
    </body>
    </html>
    

    I need ??? to be replaced for every page.

    Is this possible? If so, how? Thank you. : )

  • webbiedave
    webbiedave about 14 years
    Agreed. As his experience and needs progress, he should make the leap. My answer was just a simple starting point.
  • Mat
    Mat almost 12 years
    yes but if you define current page as "random page title" it will be "random page title" on everypage or you will have to edit every page where you want a title... in the long run this is bad. Using a database will help by allowing you to select the page title.