I recently got a project that required a really dynamic navigation.  The client wanted the ability to add and remove pages at will with no interaction from a developer. This is generally a pretty simple request, but the client has a very specific set of style guidelines. Here is a php function that I wrote to do the job.

This script is intended for people who already know about themes in wordpress, and kn0w how to use the ‘functions.php’ file.

Its pretty simple. First you have to globalize the ‘posts’ variable within the function or get_pages() wont work. There are number of params you can pass to get_pages() however I only really need to use 3. The first is what pages to exclude. I pass this in via the template files. I am only doing this because I need to exclude a particular custom page from all the navigations on the site.

Lets take a look at the others..
[php]$pages = get_pages(“exclude=$exclude&parent=0&child_of=0&sort_column=menu_order”);[/php]
In order to only display the top level pages of my site, I am passing in parent=0, this means top level, and child_of = 0, this means no children of the parent page. So now I am free to add a bunch of sub pages and know that they wont automatically blow up our custom nav, but top level pages will be added.

The next item is sort_column=menu_order… just a side note, these variable are passed like a query string so always remember to use your ampersands 🙂 The sort_column has a few options, I manually manage the page order in the WP CMS, thats what I am using.

Here is the rest of the function.
function active_nav($exclude){
global $post;
$pages = get_pages(“exclude=$exclude&parent=0&child_of=0&sort_column=menu_order”);
foreach ($pages as $row) {
echo “<li><a href=””.get_page_link($row->ID).”” class=”current”>”.$row->post_title.”</a></li>”;
echo “<li><a href=””.get_page_link($row->ID).””>”.$row->post_title.”</a></li>”;

Its pretty simple, it just checks what the current page is, then applies a class 0f “current” to that navigation item, giving us a custom active class.

Now all you do is call the class in the theme files like so:
[php]<div id=”nav”>


Here is a link to my github with this script.