View Full Version : Making a single html file for my Nav (php?)

02-16-2011, 04:06 PM
This might be a php question, I'm not really sure because I am somewhat new to development.

I am building a site that has a large and detailed navigation, with lots of subpages. I imagine that over time, I will be adding and deleting some pages, and will also need to make the corresponding change in my <ul> nav.

As it is set up now, I'll need to open 20 different pages and edit each of their code. I can see this getting very tedious, confusing, and having a large likelihood that I'll mess something up accidentally.

My question is: Is there a way of having my Header (and nav) as one single html or php file? So that I can only edit that one file, and it will change on all pages of my site (similar to editing a CSS style sheet). I am almost certain I have seen this done on Wordpress themes.

Sorry if this is a stupid question! If someone could just point me in the direction of a tutorial or tell me what this is called, that would be great.

Thank you!!


02-16-2011, 04:35 PM
Good morning sophshep,
A server side include can do what you're wanting. You can edit the one file and any of your pages that have the include would reflect that edited file.
Very handy.

Have a look at a PHP Include Tutorial here (http://www.tizag.com/phpT/include.php).

02-16-2011, 04:36 PM
My question is: Is there a way of having my Header (and nav) as one single html or php file?
Yes there is! Just make separate files to save the html markup (shouldn't be a complte document, just the only relevant <div>s or <ul>-<li>s ) of your header and nav. Then use include() (http://php.net/include) statements to include them, wherever required.

02-16-2011, 04:37 PM
Excavator saves the day! AGAIN!!!

02-16-2011, 04:46 PM
It is sort of a PHP and XHTML question. So here is how you can go about it. Create your pages with the following format or comparable. The following would be index.php

<?php include("header.inc"); ?>

<div id="nav">
<?php include("nav.inc"); ?>

<?php include("footer.inc"); ?>

Save your header file as .inc


Your footer file as .inc


Then save your navigation XHTML in the following nav.inc. All should work out for you. If you need more help just ask. All of your pages must be renamed to end with .php or this will not work.