Implementing Flexible Website Navigation with WordPress 3.0 | 2 | WebReference

Implementing Flexible Website Navigation with WordPress 3.0 | 2


Implementing Flexible Website Navigation with WordPress 3.0 [con't]

Step 4: The Code

As I mentioned earlier, using WordPress 3.0, you can build a decoupled menu structure with very little code. As this is a theme-related customization, you will need to add the code to the theme. We will be editing the following files: functions.php, header.php, sidebar.php and styles.php. (Note: In the example, I use WordPress' default theme called TwentyTen.)

If programming (in PHP) is not your forte, just copy and paste the code in the right file and things should work for you. And if PHP is one of the languages you speak, then you will appreciate the simplicity of this solution.


Add the following lines of code at the end of the functions.php file in the theme directory:

This function takes the name of the menu and returns its items as an array. We will be using it in the header.


Open the header.php file and look for the following line:

Note: If you are not using TwentyTen, then you will have to find the part of the code that is generating the HTML for the top-level links and comment it.

Replace that line with the following code:

In a nutshell, this code generates the top-level links, adding a class to the menu item that is the top-level link for the current page.


In the sidebar, we want to show the secondary links. This is achieved with the following lines of code:

Add the above code at the top of the sidebar.php file.

That's it! In the browser, go to the URL where you have installed WordPress and see the navigation in action. There is one last thing that needs to be done though.


Add the following CSS at the end of the styles.css file so that the secondary links appear properly and the current page is styled differently.

WordPress 3.0: Blog Platform to Full CMS

Many people consider WordPress a content management system (CMS) and I do too, but with an asterisk. Why? Because it is primarily a blogging tool. While there are several plugins to help you take it beyond just a blog, the backend interface and the navigation system is extremely blog-centric.

With the release of WordPress 3.0, I can definitely see a shift in the positioning of the tool. With the introduction of custom menus and custom page types, I am ready to erase the asterisk and move WordPress toward the top of my CMS list. The options that CMS products such as Drupal and Joomla offer may be superior to WordPress, but WordPress has the advantage of an easy-to-use backend interface.