Have you ever spent hours amending a menu structure manually throughout a website, having to amend each page one by one. Well theres a very easy way to use a simple PHP function to display the contents of a file, wherever you want on a website. So for example - your navigation, you can have this in a single file, and call it to display on every page, and when you want to add or amend a link, you just edit the one file and it will cascade down to all other pages.
In order for this to work there’s a couple of things you need to check/do.
You will need to have PHP installed and running on your web server, and all your web files will need to have the extension .php instead of .html/.htm etc.
If ok, then lets begin.
Create a new web file in notepad or your normal editing software, and name it nav.inc.php.
Now in this file you can have whatever you want, and it will be displayed on the page where you call it. For the purpose of this example were going to use a small menu, so we are going to cut the menu from our normal html page and paste it into our nav.inc.php file.
Save the file. I've created a folder called includes and saved the file in there to keep everything tidy.
The contents of the file nav.inc.php should now contain similar code to the below image, Just the menu, nothing else.

Now in place of your menu that you just cut out of your normal web page, you need to add the following php function.

This will pull through the contents of nav.inc.php where you place it.
So your php(renamed) page should contain the following:

You will need to replace the menu with the include call on all pages that you will need the menu.
Thats it. If you did everything as stated above, you should now be able to amend the menu from a single file and it will cascade down to the rest of the website.

Leave your comment