There are several new features for theme authors in WordPress 2.7 that should be covered, but I believe one that might be useful is the new wp_page_menu() function.
It’s nothing special, but it does take a lot of the work out of coding that we do over and over in every theme we create when using the wp_list_pages() function.
Here’s an example of how to use it:
<?php wp_page_menu('show_home=Home&menu_class=page-nav'); ?>
This automatically adds a div with the ID of page-nav around an unordered list, which also includes a link back to the home page.
The output basically looks like this:
<div id="page-nav">
<ul>
<li class="current_page_item"><a href="#">Home</a></li>
<li class="page_item page-item-1"><a href="#" title="Example">Example</a></li>
<li class="page_item page-item-2"><a href="#" title="Example">Example</a></li>
<li class="page_item page-item-3"><a href="#" title="Example">Example</a></li>
</ul>
</div>
It just wraps up some of that code that we normally have to write.
Change output from child theme or plugin
If you’re not looking to dig into theme files to change the output, you can use a filter to change the arguments.
For example, you could add this to your child theme’s functions.php file:
<?php
add_filter('wp_page_menu_args', 'my_page_nav');
function my_page_nav($args) {
$args = array(
'show_home' => false,
'menu_class' => 'navigation',
'depth' => 3,
);
return $args;
}
?>
Backward compatibility
If you want to use this functionality but have your theme work with older versions of WordPress, just use this code:
<?php if(function_exists('wp_page_menu')) : ?>
<?php wp_page_menu('show_home=Home&menu_class=page-nav'); ?>
<?php else : ?>
<div id="page-nav">
<ul>
<?php wp_list_pages('title_li='); ?>
</ul>
</div>
<?php endif; ?>

Thanks a lot for the information. It does simplifies some of the work.
Dude, you’re starting to freak me out!
This is exactly what I did in the new theme I’m working on. The only difference is that I did a function check for wp_page_menu and if it doesn’t exist, I create it, and let it accept the arguments as normal. I then parse the arguments and create a page menu based on the arguments (menu class, show home, etc.) using wp_list_pages.
Very cool stuff. I’ll probably write up a tutorial on how I did the backward compatibility sometime soon.
Good stuff, Justin. Keep it coming!
Nathan
Yet another nice tip, man. Thanks for sharing.
WordPress is becoming more easier saving time. This is ‘write less, do more’.
Somehow I missed this one. Very handy. Thanks for the info. Backwards compatibility on some of the other new functions could be tricky as well.
Mayur Somani
No problem.
wp_page_menu()does make things a lot simpler.Nathan
My Hybrid theme actually creates
wp_page_menu()if the function doesn’t exist. It’s cleaner that way, but I didn’t want to get too complicated with this tutorial.You should definitely write up that tutorial on your backward compatibility method because it’s much better for advanced theme authors.
J Mehmett
I like the write less, do more motto when it comes to these neat features.
I even made a function exactly like this for categories in my new Hybrid theme.
Lyndi
The backward compatibility isn’t too tough. The biggest one is the comments function that I covered.
I suspect that if we get this many new theme enhancements in 2.8, backward compatibility will start getting pretty rough.
Wow, this is what I looking for. Thanks justin
Justin, just a quick question please. Is it possible to add a ‘site-admin’ link to the end of the menu generated with this function?
Jauhari
No problem. I’m glad to be of service.
Lyndi
I think you’ll be better off using the
wp_list_pages()function and tacking on a link for that. This is just for generating a menu of your WordPress pages.Is there any chance that wp_dropdown_pages() can use the show_home option? In the last reply you mention taking on a link to the output of wp_list_pages()…are there examples available showing how to do that? Thanks,
Ralph
Just found this one.
Great! Thanks so much
Thanks for the info. I was looking for an easier way to do this
Oh, and I like the way your posts read. Things are always where I expect to find them. Nice work.
Any way to remove a particular page with this?
Thanks.
Thanks for the tutorial, this time I’m looking for this actually for my new blog.
Thanks once more.
same here, any way to remove a page from the menu? I can’t imagine that they would have left that ability out.
Great article. So basically if im converting a site from html to wordpress, all I have to do is create the dropdown menu with css and get it to look the way I want it in html, then just use the above code where i want the menu to appear, and my css will make it work and look like in the html? Is this correct? its that simple?
Norman