centralize html menu with includes
- Started
- Last post
- 11 Responses
- bort
I'm building a static html site, the first in a while, with about 3 dozen pages that go three levels deep. Ideally, I'd like some way to keep my main nav code in one file so it can be changed globally rather than plugging through 36 pages if something changes. I came across some tutorials that suggested achieving this through php includes, but haven't managed to get it to work yet. Anyone have any suggestions on the best technique or direct me towards a good tutorial. The rest of the site will be hard coded and doesn't need any special attention.
- brandelec0
you can do a <?php include("menu.php"); ?>
and in menu.php is your menu
- Stugoo0
you can use library items if you dont know any php or server side scripts
or
use a server side include using asp, cold fusion php, etc
- Nairn0
- Save the snippet of code you want, sans headers of any kind as 'menu.php' or whatever.
- layout your page, saved with a '.php' extension.
- where you want the snippet of code included write:
<?php include('./menu.php'); ?>
- save file, then view via a php server (so not just on your local PC, if it's not got something like Xampp running!)
- bort0
That's exactly what I did, but once I'm on a page in a sub folder (one level removed from the root) my links return 404's. How do I ensure the menu links are updated according to what page is being viewed? Right now I'm using relative links.
- All links work from the root index.php, broken from all other pagesbort
- well you'll need to point it to the right path.Jnr_Madison
- myfolder/menu.phpJnr_Madison
- brandelec0
you're not seeing it cos ur a level down still looking for menu.php instead of ../menu.php
try putting the full path of menu.php instead of relative link
- acescence0
you just need to prepend the current directory, this will help you...
- Stugoo0
you could use absolute paths :
if you put your menu navigation in a folder called "_includes"
following:
the absolute path for a file in domian.com would be/_includes/navigation.php
would be
<?php include("/_includes/navigation.php"); ?>
read :
http://webdesign.about.com/od/be…
or
http://en.wikipedia.org/wiki/Pat…)basically in relative paths "../" means "go up one level"
- if you wanted to be smart you could use a variable for the "../" and attach it to you URLSStugoo
- bort0
Okay. First off, thanks for the help.
Here's what I have in my root index.php
<?php include ('includes/main_nav.php'); ?>Here's what I have in my about sub directory page about.php (1 level removed from the root)
<?php include ('../includes/main_nav.php'); ?>Here's the main_nav.php file
<ul id="menu">
<li><a href="/about/about.php">About us</a>
<ul class="drop">
<li><a href="/about/what-we-do.php">What we do</a></li>
</ul>Thing is, when I try and go from About us to What we do, I get this path instead: root/about/about/what-we-do.php
when really what I should be getting is
root/about/what-we-do.php
Does every file have to be in a separate folder or something?
- brandelec0
don't bother with subfolders for your pages
even your main_nav.php doesn't have to be in an includes folderyou'll save yourself a lot of time and headaches if you get rid of folders
- bort0
Really? On a site with 36+ pages wouldn't it be advantageous to have some structure?