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.
[...] the original post: New page menu function in WordPress 2.7 This entry was posted on Tuesday, November 11th, 2008 and is filed under Wordpress News. You can [...]
[...] New page function menu in WordPress 2.7 (tags: WordPress 2.7 tips tutorial) [...]
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.
[...] read this new page menu function on Justin Tadlock’s blog and I wanted to share.. There are several new features for theme authors in WordPress [...]
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
[...] authors to use to get their themes updated. Justin Tadlock recently wrote a great post about the new page menu function in WordPress 2.7 and explains how to save some time when coding the wp_list_pages() function. [...]
[...] Justin Tadlock has a detailed tutorial on integrating this function into your WordPress 2.7-ready themes. Be sure to make them backwards-compatible too! [...]
[...] to Justin Tadlock for this! Did I helped you? If yes, feel free to make a small [...]
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.[...] New page menu function in WordPress 2.7 (tags: wp27) [...]
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
[...] New Page Menu in WordPress 2.7 Another ustin Tadlock article shows you how the new function wp_page_menu() removes a lot of the work you may do with using wp_list_pages() function. [...]
[...] New Page Menu in WordPress 2.7(WordPress 2.7 的新页面菜单) 另一篇 Tadlock 的文章告诉你新的函数 wp_page_menu() 移走了之前需要使用 wp_list_pages() 函数做的大量工作。 [...]
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.