Function Reference/wp list pages

(重定向自Template Tags/wp list pages
跳转至: 导航搜索


Displays a list of WordPress Pages as links. It is often used to customize the Sidebar or Header, but may be used in other Templates as well.


%%% <?php wp_list_pages( $args ); ?> %%%

Default Usage

<?php $args = array(
	'depth'        => 0,
	'show_date'    => '',
	'date_format'  => get_option('date_format'),
	'child_of'     => 0,
	'exclude'      => '',
	'include'      => '',
	'title_li'     => __('Pages'),
	'echo'         => 1,
	'authors'      => '',
	'sort_column'  => 'menu_order, post_title',
	'link_before'  => '',
	'link_after'   => '',
	'walker'       => '',
	'post_type'    => 'page',
        'post_status'  => 'publish' 
); ?>

By default, the usage shows:

  • All Pages and sub-pages are displayed (no depth restriction)
  • Date created is not displayed
  • Is not restricted to the child_of any Page
  • No pages are excluded
  • No pages are explicitly included
  • The title of the pages listed is "Pages"
  • Results are echoed (displayed)
  • Is not restricted to any specific author
  • Sorted by Page Order then Page Title.
  • Sorted in ascending order
  • Pages displayed in a hierarchical indented fashion (not shown in defaults above)
  • Includes all Pages (not shown in defaults above)
  • Not restricted to Pages with specific meta key/meta value (not shown in defaults above)
  • No Parent/Child trees excluded


Note: generically, arguments are inherited from get_pages.

(string) Sorts the list of Pages in a number of different ways. The default setting is sort alphabetically by Page title.
  • 'post_title' - Sort Pages alphabetically (by title) - default
  • 'menu_order' - Sort Pages by Page Order. Note the difference between Page Order and Page ID. The Page ID is a unique number assigned by WordPress to every post or page. The Page Order can be set by the user in the Pages→Edit panel. See the example below.
  • 'post_date' - Sort by creation time.
  • 'post_modified' - Sort by time last modified.
  • 'ID' - Sort by numeric Page ID.
  • 'post_author' - Sort by the Page author's numeric ID.
  • 'post_name' - Sort alphabetically by Post slug.
Note: The sort_column parameter can be used to sort the list of Pages by the descriptor of any field in the wp_post table of the WordPress database. Some useful examples are listed here.
(string) Change the sort order of the list of Pages (either ascending or descending). The default is ascending. Valid values:
  • 'ASC' - Sort from lowest to highest (Default).
  • 'DESC' - Sort from highest to lowest.
(string) Define a comma-separated list of Page IDs to be excluded from the list (example: 'exclude=3,7,31'). There is no default value. See the Exclude Pages from List example below.
(string) Define a comma-separated list of parent Page IDs to be excluded. Use this parameter to exclude a parent and all of that parent's child Pages. So 'exclude_tree=5' would exclude the parent Page 5, and its child (all descendant) Pages. (This parameter was added with Version 2.7, and it does not function as described as of version 2.8.1 - See bug report.)
(string) Only include certain Pages in the list generated by wp_list_pages. Like exclude, this parameter takes a comma-separated list of Page IDs. There is no default value. See the Include Pages in List example below.
(integer) This parameter controls how many levels in the hierarchy of pages are to be included in the list generated by wp_list_pages. The default value is 0 (display all pages, including all sub-pages).
  • 0 (default) Displays pages at any depth and arranges them hierarchically in nested lists
  • -1 Displays pages at any depth and arranges them in a single, flat list
  • 1 Displays top-level Pages only
  • 2, 3 … Displays Pages to the given depth
(integer) Displays the sub-pages of a single Page only; uses the ID for a Page as the value. Note that the child_of parameter will also fetch "grandchildren" of the given ID, not just its immediate children. Defaults to 0 (displays all Pages).
(string) Display creation or last modified date next to each Page. The default value is the null value (do not display dates). Valid values:
  • '' - Display no date (Default).
  • 'modified' - Display the date last modified.
  • 'xxx' - Any value other than modified displays the date (post_date) the Page was first created. See the example below.
(string) Controls the format of the Page date set by the show_date parameter (example: "l, F j, Y"). This parameter defaults to the date format configured in your WordPress options. See Formatting Date and Time and the date format page on the php web site.
(string) Set the text and style of the Page list's heading. Defaults to '__('Pages')', which displays "Pages" (the __('') is used for localization purposes). If passed a null or empty value (''), no heading is displayed, and the list will not be wrapped with <ul>, </ul> tags. See the example for Headings.
(boolean) Toggles the display of the generated list of links or return the list as an HTML text string to be used in PHP. The default value is 1 (display the generated list items). Valid values:
  • 1 (True) - default
  • 0 (False)
(string) Only include the Pages that have this Custom Field Key (use in conjunction with the meta_value field).
(string) Only include the Pages that have this Custom Field Value (use in conjunction with the meta_key field).
(string) Sets the text or html that precedes the link text inside <a> tag. (This parameter was added with Version 2.7)
(string) Sets the text or html that follows the link text inside <a> tag. (This parameter was added with Version 2.7)
(string) Only include Pages authored by the authors in this comma-separated list of author IDs. If no authors are specified, all authors are included.
(integer) Sets the number of Pages to display. This causes the SQL LIMIT value to be defined. Default to no LIMIT. (This parameter was added with Version 2.8) NOTE: this is currently not working, see
(integer) The number of Pages to pass over (or displace) before collecting the set of Pages. Default is no OFFSET. (This parameter was added with Version 2.8)
(string) List posts associated with certain type. Default is 'page'. Valid values include:
  • post' - a post.
  • 'page' - a page.
  • 'revision' - a revision.
  • 'attachment' - an attachment.
  • Hierarchical Custom Post Types.
a comma-separated list of all post status types to return. For example: 'publish,private'


Hiding or Changing the List Heading

The default heading of the list ("Pages") of Pages generated by wp_list_pages can be hidden by passing a null or empty value to the title_li parameter. The following example displays no heading text above the list.

<?php wp_list_pages('title_li='); ?>

In the following example, only Pages with IDs 9, 5, and 23 are included in the list and the heading text has been changed to the word "Poetry", with a heading style of <h2>:

  <?php wp_list_pages('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?>

List Pages by Page Order

The following example lists the Pages in the order defined by the Order setting for each Page in the Pages→Edit panel.

  <?php wp_list_pages('sort_column=menu_order'); ?>

If you wanted to sort the list by Page Order and display the word "Prose" as the list heading (in h2 style) on a Sidebar, you could add the following code to the sidebar.php file:

  <?php wp_list_pages('sort_column=menu_order&title_li=<h2>' . __('Prose') . '</h2>' ); ?>

Using the following piece of code, the Pages will display without heading and in Page Order:

  <?php wp_list_pages('sort_column=menu_order&title_li='); ?>

Sort Pages by Post Date

This example displays Pages sorted by (creation) date, and shows the date next to each Page list item.

  <?php wp_list_pages('sort_column=post_date&show_date=created'); ?>

Exclude Pages from List

Use the exclude parameter to hide certain Pages from the list to be generated by wp_list_pages.

  <?php wp_list_pages('exclude=17,38' ); ?>

Include Pages in List

To include only certain Pages in the list, for instance, Pages with ID numbers 35, 7, 26 and 13, use the include parameter.

  <?php wp_list_pages('include=7,13,26,35&title_li=<h2>' . __('Pages') . '</h2>' ); ?>

List Sub-Pages

Versions prior to Wordpress 2.0.1 :

Put this inside the the_post() section of the page.php template of your WordPress theme after the_content(), or put it in a copy of the page.php template that you use for pages that have sub-pages:

  global $id;
  &date_format=$date_format"); ?>

This example does not work with Wordpress 2.0.1 or newer if placed in a page template because the global $id is not set. Use the following code instead.

Wordpress 2.0.1 or newer :

NOTE: Requires an HTML tag (either <ul> or <ol>) even if there are no subpages. Keep this in mind if you are using css to style the list.

  &date_format=$date_format'); ?>

The following example will generate a list only if there are child pages (pages that designate the current page as a parent) for the current page:

  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
  if ($children) { ?>
  <?php echo $children; ?>
  <?php } ?>

List subpages even if on a subpage

The above examples will only show the children from the parent page, but not when actually on a child page. This code will show the child pages, and only the child pages, when on a parent or on one of the children.

This code will not work if placed after a widget block in the sidebar.

  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <?php echo $children; ?>
  <?php } ?>

As an alternative, this code in a sidebar.php, displays only top level Pages, but when viewing a Page that has children (or is a child) it displays only children of that parent.

  • When visiting main page, all top level pages are listed in the sidebar.
  • When visiting a top level page with no children, all top level pages are listed.
  • When visiting a top level page with children, just the children pages, and descendant pages, are listed.
  • When visiting a child page, just the children, and descendant pages, of that parent, are listed.
$output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
if (is_page( )) {
  $page = $post->ID;
  if ($post->post_parent) {
    $page = $post->post_parent;
  $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
  if ($children) {
    $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
echo $output;
  • Page List that: Only displays if child (sub) pages exist, displays page list of subpages on the parent page AND on the child pages HOWEVER this code keeps the parent page name in the title which makes it different from the previous example.)
  if($post->post_parent) {
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  $titlenamer = get_the_title($post->post_parent);

  else {
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  $titlenamer = get_the_title($post->ID);
  if ($children) { ?>

  <h2> <?php echo $titlenamer; ?> </h2>
  <?php echo $children; ?>

<?php } ?>

List whole subpages

This is how to get the whole subpages list

	// will display the subpages of this top level page
	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");

	if($post->ancestors) {
		// now you can get the the top ID of this page
		// wp is putting the ids DESC, thats why the top level ID is the last one
		$ancestors = get_post_ancestors($this_page);
		$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");

if ($children) { ?>
		<?php echo $children; ?>
<?php } ?>

List parent Page and all descendant Pages

Since there's no way to tell wp_list_pages to display a particular parent tree (parent and all generations displayes) this example uses get_pages to get all the descendants for a parent, then use wp_list_pages to display the 'family tree'.

// use wp_list_pages to display parent and all child pages all generations (a tree with parent)
$parent = 93;
  'child_of' => $parent
$pages = get_pages($args);  
if ($pages) {
  $pageids = array();
  foreach ($pages as $page) {
    $pageids[]= $page->ID;

    'title_li' => 'Tree of Parent Page ' . $parent,
    'include' =>  $parent . ',' . implode(",", $pageids)

List current Page with its ancestors and children

This example will list current Page, ancestors of current page, and children of current page, Since child_of displays all children, wpdb is used instead, along with include to not display everything 'and the kitchen sink'.

//if the post has a parent
  //collect ancestor pages
  $relations = get_post_ancestors($post->ID);
  //get child pages
  $result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
  if ($result){
    foreach($result as $pageID){
      array_push($relations, $pageID->ID);
  //add current post to pages
  array_push($relations, $post->ID);
  //get comma delimited list of children and parents and self
  $relations_string = implode(",",$relations);
  //use include to list only the collected pages. 
  $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
  // display only main level and children
  $sidelinks = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID);

if ($sidelinks) { ?>
  <h2><?php the_title(); ?></h2>
    <?php //links in <li> tags
    echo $sidelinks; ?>
<?php } ?>

List topmost ancestor and its immediate children

This method will show the topmost ancestor of the current page as well as the topmost ancestor's immediate children. This can be used for clean secondary subnavigation.

First, create the following function (preferably in functions.php, assuming this is for a theme):

 * Gets the id of the topmost ancestor of the current page. Returns the current
 * page's id if there is no parent.
 * @uses object $post
 * @return int 
function get_post_top_ancestor_id(){
    global $post;
        $ancestors = array_reverse(get_post_ancestors($post->ID));
        return $ancestors[0];
    return $post->ID;

Next, add the following code to your theme (wherever you want the menu to appear):

<ul class="clearfix">
    <?php wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) ); ?>
    <?php wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) ); ?>

List the Home page

If you want a link to the blog page as well use wp_page_menu().

List members of a custom post type

If a given custom post type is hierarchical in nature, then wp_list_pages() can be used to list the member of that custom post type. In this example the custom post type Portfolio is listed:

$args = array(
  'title_li'=> __('Portfolio')
wp_list_pages( $args ); 

Markup and styling of page items

By default, wp_list_pages() generates a nested, unordered list of WordPress Pages created with the Write > Page admin panel. You can remove the outermost item (li.pagenav) and list (ul) by setting the title_li parameter to an empty string.

All list items (li) generated by wp_list_pages() are marked with the class page_item. When wp_list_pages() is called while displaying a Page, the list item for that Page is given the additional class current_page_item.

<li class="pagenav">
Pages [title_li]
    <!-- Output starts here if 'title_li' parameter is empty -->
    <li class="page-item-2 page_item current_page_ancestor current_page_parent">
      [parent of the current Page]
        <li class="page-item-21 page_item current_page_item">
          [the current Page]
    <li class="page-item-3 page_item">
      [another Page]

They can be styled with CSS selectors:

.pagenav { … } /* the outermost list item; contains whole list */
.page-item-2 { … } /* item for Page ID 2 */
.page_item { … } /* any Page item */
.current_page_item { … } /* the current Page */
.current_page_parent { … } /* parent of the current Page */
.current_page_ancestor { … } /* any ancestor of the current Page */

In order to achieve an accordion menu effect for instance, the following CSS can be used:

.pagenav  ul ul,
.pagenav .current_page_item ul ul,
.pagenav .current_page_ancestor ul ul,
.pagenav .current_page_ancestor .current_page_item ul ul,
.pagenav .current_page_ancestor .current_page_ancestor ul ul {
	display: none;

.pagenav .current_page_item ul,
.pagenav .current_page_ancestor ul,
.pagenav .current_page_ancestor .current_page_item ul,
.pagenav .current_page_ancestor .current_page_ancestor ul,
.pagenav .current_page_ancestor .current_page_ancestor .current_page_item ul,
.pagenav .current_page_ancestor .current_page_ancestor .current_page_ancestor ul {
	display: block;

Menu Item CSS Classes

The following classes are applied to menu items, i.e. to the HTML <li> tags, generated by wp_list_pages(). Note: The wp_list_pages() and wp_page_menu() functions output the same CSS classes.

All Menu Items

  • .page_item
    This class is added to menu items that correspond to a static page.
  • .page-item-$ID
    This class is added to menu items that correspond to a static page, where $ID is the static page ID.

Current-Page Menu Items

  • .current_page_item
    This class is added to menu items that correspond to the currently rendered static page.

Current-Page Parent Menu Items

  • .current_page_parent
    This class is added to menu items that correspond to the hierarchical parent of the currently rendered static page.

Current-Page Ancestor Menu Items

  • .current_page_ancestor
    This class is added to menu items that correspond to a hierarchical ancestor of the currently rendered static page.



  • Since: 1.5.0
  • 2.7 : Added the link_before, link_after and exclude_tree parameters.
  • 2.8 : Added the number and offset parameters.

Source File

wp_list_pages() is located in onlyincludecodewp-includes/post-template.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on


  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.


Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre



wp_list_authors, wp_list_categories, wp_list_pages, wp_list_bookmarks, wp_list_comments, wp_get_archives, wp_page_menu, wp_dropdown_pages, wp_dropdown_categories, wp_dropdown_users

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude


This Template is used by Codex:Template Messages.


pre 检查到模板循环:模板:Message /pre