PHP – Dynamic Navigation Bar Based on Files in Folder

Thie php script automatically generates a navigation menu based on the pages in the same directory.

The php script does the following:

  • Loops through all the files in the directory looking for php, html and htm files
  • Uses the PHP Simple HTML DOM Parser to find the page h2 title with id=”maintitle”
    Note: you can set it to find any tag on the page
  • It stores the page title and the page URL
  • It alphabetizes the pages and displays the into a <li> list
  • Using jQuery the current <li> item class is set to “active”

Demo uses a modified version of a free template from CSS Heaven

Also made the loopedslider dynamic so it dynamically grabs all the images in the slideshow directory and reads the image metadata for the title and text.


In order for the example to work below you will need to add the following code to each of your web pages. This is what the script looks for and makes it the text part of the sidebar links

Here is the auto navigation script. I recommend putting it into an include file on all your pages.

Trackback from your site.

Leave a comment