My First Approved Script on CodeCanyon.net
May, 2010
09

Setting an ‘Active’ Class for Dynamic Navigations in WordPress

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..

$pages = get_pages("exclude=$exclude&parent=0&child_of=0&sort_column=menu_order");

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) {
 if(is_page($row->ID)){
 echo "<li><a href=\"".get_page_link($row->ID)."\" class=\"current\">".$row->post_title."</a></li>";
 }else{
 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:

<div id="nav">
 <ul>                                   
 <?php 

 active_nav(your_page_id_int);

 ?>
 </ul>
 </div>

Here is a link to my github with this script.

Cheers

Post to Twitter Tweet This!

This entry was posted on 05.09.2010 at 10:01 pm and is filed under Code Bin. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comments

3 Responses to “Setting an ‘Active’ Class for Dynamic Navigations in WordPress”

05.09.2010 | 10:04 pm

[...] This post was mentioned on Twitter by Sam Napolitano. Sam Napolitano said: New blog post: Setting an 'Active' Class for Dynamic Navigations in WordPress http://tinyurl.com/39le9g8 [...]

Benjamin Welch

06.25.2010 | 5:52 am

Hey Sam,

I used your function above and it worked great. However, when I go to the blog page, it doesn’t apply the current class to it. But it does with every other page.

My home is set to a static page. The blog function of wordpress in on the blog page.

Any thoughts on why that would happen?

07.01.2010 | 2:40 am

Hey Ben,

I am actually kind of stumped by this, I have used it in many sites now with the same configuration that you are describing.

The only thing I could think of is that you have to actually create a page call blog, then in the settings (under reading) set the ‘posts’ page to that page…

Let me know how it works out.

-Sam

Leave a Reply

Green Web Hosting! This site hosted by DreamHost.