Navigation Menu Includes

Simple php includes can make site navigation updates a breeze.

When you do alot of updating to your site, it gets super tedious to have to re-work your navigation on each . . . and . . . every . . . single . . . page effected by the update.

On my site's current incarnation, I have the main navigation that stays fixed, but the interior navigation changes according to what page you are on and what section you are in.

Simple to write. Simple to use.

  • Cut your current navigational HTML markup from your page.
  • Paste it on its own page.
  • Wrap some php around it to create a function.
  • Include the function in your HTML document.
  • Call the function where your old navigation markup used to be.

Anytime you change or add to your navigation, you only have to edit that one file. No more tedious, multiple page edits.

How to do this on your site

Let's say this is the HTML of your navigation menu. Cut it out of your current HTML document and paste it on a new, blank page.

<div id = "sidebar">
<ul id = "sideNav">
<li><a href="#">MD5 Password encrypt</a></li>
<li><a href="#">Passing values in URL</a></li>
<li><a href="#">Alternating row colors in a table</a></li>
<li><a href="#">Countdown Script</a></li>
<li><a href="#">Organizing the output of an array</a></li>
</ul>

</div>

Since you'll be creating a custom php function, you won't need the <html>, <body> or any other HTML tags with it. Do it like this:

<?php
// function to include my interior navigation menu in the php section of my website

function phpMenu() {

echo '<ul id = "sideNav">';
echo '<li><a href="#">MD5 Password encrypt</a></li>';
echo '<li><a href="#">Passing values in URL</a></li>';
echo '<li><a href="#">Alternating row colors in a table</a></li>';
echo '<li><a href="#">Countdown Script</a></li>';
echo '<li><a href="#">Organizing the output of an array</a></li>';
echo '</ul>';

}

There are numerous ways you could save and locate this file. I prefer to put it in a folder called 'includes'. I would name the file: phpMenu.inc.php. Now, on the pages that you want to have this menu you'll need to do 3 things.

  • Include the file in your HTML document.
  • Call the phpMenu() function where you want your navigation.
  • Rename your HTML page as a PHP page with the .php extension.

Including the file is easy. I prefer to put it in the head of the document:

<?php
include 'includes/phpMenu.inc.php';
?>
<html>
<body>

<p>Lorem ipsum qui.....</p>

Calling the function is easy as well. Right in your <div = "sidebar"> where you originally cut it out put:

<div id="sidebar">
<?php
phpMenu();
?>
</div>

Finally, rename your HTML page with a .php extension.

There you have it! Now, any navigation changes you make to the phpMenu.inc.php file will be displayed on all the pages you have included it in.