PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Static Page Navigation

Posted by on 28th Dec 2008 WordPress Coding & Design 11 comments

A new template tag was introduced for WordPress 2.7 to provide different functionality for generating your page navigation. For earlier versions, and still for WordPress 2.7, we have wp_list_pages(), however for 2.7 we now have wp_page_menu().

This function simplifies some of the aspects of generating a list of static pages. The parameters that it takes in are:

sort_column
Sorts the pages into the specified order. Values can be post_title, menu_order, post_date, post_modified, ID, post_author, post_name (sorts by the slug)
menu_class
Specifies the class given to the div that is output around the menu
echo
Accepts 0 or 1 to control whether the menu is output or returned for use within your PHP. 1 means output, 0 means don’t
show_home
Accepts 0 or 1 to create a Home link. 1 means create the link, 0 means don’t
link_before
Sets the text or HTML that preceeds the anchor text
link_after
Sets the text or HTML that follows the anchor text

A simple usage of this would then be:

<?php wp_page_menu('sort_column=menu_order&menu_class=nav&show_home=1&echo=1'); ?>

This would then give us:

[sourcecode language="html"]

[/sourcecode]

To achieve the same output using the old wp_list_pages() function would require:

[sourcecode language="php"]

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

11 comments - Leave a reply
  • Posted by Blog Expert on 28th Dec 2008

    Awesome tutorial. You guys have had plenty of these tutorials but I am absolutely loving them.

  • Posted by love bebo skins on 28th Dec 2008

    thanks cool tutorial. was easy enough to follow

  • Posted by Tobias Fransson on 29th Dec 2008

    Nice, not sure that I will use it though but I love it when you get added flexibility and easy to use extra functions.

  • Posted by Sarah on 29th Dec 2008

    Glad you liked the post. Personally I think I'll start changing custom designs to use this code as it's simply less code which is always a good thing :)

  • Posted by Dan Schulz on 29th Dec 2008

    Sarah, is it possible to remove that stupid pointless and annoying DIV tag from around the list menu? My god, it's almost 2009 – you'd think by now that people (I'm looking squarely at the WordPress developers here) would know that a list is a block-level element and as such does not need a DIV around it!

  • Posted by Agnus on 29th Dec 2008

    As this involves lot of coding it takes some time to understand..But i always like to implement new updates in the site navigation which is my interest..

  • Posted by Sarah on 2nd Jan 2009

    @Dan I've not looked into it yet, although I would imagine you could with an action as you can manipulate the old template tag in that way. I agree it's a bit daft to have the div around it. At first I thought/hoped the class name would go into the actual ul tag, which would obviously make more sense.

    I'll try and write a function for it to go into the functions.php file and post it up here, which will put the class into the ul and remove the div completely. Actually, I'll use that as the basis for Sunday's post :D

  • Posted by Sarah on 2nd Jan 2009

    I tried out an additional parameter that isn't documented on the Codex, which is depth. This works in the same way as in wp_list_pages, so to just get parent pages listed, use depth=1.