phphtmlincludexamppget-headers

Include external code on a web page ( php include() or get_header() )


I've started creating a custom html page for the first time and i've hit a bump. The code in the header will be the same in all my pages , so i want to put it in an external file (header.php) and just put 1 line of code in all my pages to link to it but it doesn't seem to be working .

HTML file :

<body>

<?php include('header.php') ?>     // didn't work
<?php get_header(); ?>      // didn't work

<div class="content"> </div>

</body>

PHP File :

<h1>TITLE</h1>

When i put the code directly on the page - it shows "TITLE"

When i try include or get_header , i get a blank page . I checked the codex and online posts but i've not been able to fix it .

Any help would be appreciated - Thanks


Solution

  • Try in your header.php:

    <?php
    echo "<h1>TITLE</h1>";
    ?>
    

    Then where you want to insert the header:

    <?php include '/header.php' ?>
    

    The / character makes the path to header.php relative to your root.