New page menu function in WordPress 2.7

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; ?>

Resources