0

I didn't really know how to phrase this question but this is what I'm trying to ask. How can I make it so that this block of code is on every page but I only have to edit the original block of code so that it changes on all pages?

<ul class="horizontal" id="nav">
    <li><a href="#" class="active">About Us</a></li>
    <li><a href="#">Our Products</a></li>
    <li><a href="#">FAQs</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="#">Login</a></li>
</ul>

1 Answers1

1

Use PHP. Then you can create a file called (for example) header.php

In header.php:

<ul class="horizontal" id="nav">
    <li><a href="#" class="active">About Us</a></li>
    <li><a href="#">Our Products</a></li>
    <li><a href="#">FAQs</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="#">Login</a></li>
</ul>

Then on any page you want to include this section you can simply write:

<?php include 'header.php'?>

In place of where you would have put this block of code. Any changes to header.php will update on any pages using it.

For example:

<html>
    <head>
        <title>My page</title>
    </head>
    <body>
        <h1>My page</h1>
        <?php include 'header.php'?>
        <img src='image.png'/>
        <p>This is my webpage</p>
    </body>
</html>
mbdavis
  • 3,089
  • 2
  • 17
  • 34
  • Does this mean that I will have to create a different file for each different block of code. eg- one file for header, one for main content, one for the footer? – John F. Cook Mar 28 '14 at 17:32
  • Yes, unless they always follow each other on your pages – IronWilliamCash Mar 28 '14 at 17:36
  • @JohnF.Cook it makes no difference, you can write your pages exactly as normal, but just if you want to make reusable chunks you can do so by splitting off into other files such as the header. – mbdavis Mar 28 '14 at 17:38