wp_list_pages()
Outputs a list of static pages as links.
Usually used in header.php (site header) or sidebar.php (sidebar) to create a menu.
See also a very similar function wp_page_menu().
An alternative for creating a menu is the function wp_nav_menu(), added in version 3.0. Read the manual here.
Hooks from the function
Returns
null|String.
Usage template
wp_list_pages( [
'authors' => '',
'child_of' => 0,
'date_format' => get_option('date_format'),
'depth' => 0,
'echo' => 1,
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'link_after' => '',
'link_before' => '',
'meta_key' => '',
'meta_value' => '',
'number' => '',
'offset' => '',
'post_type' => 'page', // see get_pages()
'show_date' => '',
'sort_column' => 'menu_order, post_title',
'sort_order' => 'ASC',
'title_li' => __('Pages'),
'walker' => '',
] );
Usage
<?php wp_list_pages( $args ); ?>
- $args
- Parameters by which to get the list of pages.
Arguments of the $args parameter
In addition to the parameters below, the function can accept all the same parameters as get_pages(), because it works based on it.
- depth(int)
This parameter controls the level of nesting of child pages that will be included in the list. Default is 0 (show all child pages, including double and more nesting).
0(default) Include all levels of nested pages in the list and show them in a tree view.-1Include all levels of nested pages, but do not show nesting (tree view of the list is disabled, the list will be shown as flat).1Show only the first level child pages, i.e., first-level child pages.2, 3, etc.Include child pages of level 2, 3, etc.…
Default: 0
- show_date(string)
Shows the creation or modification date of the page next to the link. By default, date display is disabled.
''- do not show dates (default).modified- show the modification date.created- show the creation date of the page.
Default: null
- date_format(string)
- Controls the format in which the date will be displayed if the
show_dateparameter is enabled, for example,d/m/Ywill output: 10/11/2011
Default: WordPress date settings - child_of(int)
- Show only child pages of a specific page indicated in this parameter. You need to specify the ID of the page whose subpages you want to display. Default is 0 - show all pages.
Default: 0 - exclude(string)
- In this parameter, specify the IDs of the pages that you do not want to be included in the list, for example:
exclude=3,7,31.
Default: '' - exclude_tree(string)
- Specify the parent page IDs that you want to exclude from the list, separated by commas. All nested (child) pages of the specified IDs will also be excluded. That is, this parameter excludes the entire tree of pages from the list.
Added in version 2.7.
Default: '' - include(string/array)
Show only the specified pages in the list. IDs can be specified as a comma or space-separated string:
include=45 63 78 94 128 140.This parameter overrides the parameters related to list formation, as it creates a list only from the specified pages. The parameters: exclude, child_of, depth, meta_key, meta_value, authors are overridden.
Default: ''- title_li(string)
- List title. Default:
__('Pages')__('')is needed for localization. If this parameter is set to empty (''), the list title will not be displayed, and the HTML tags surrounding the list (<ul>, </ul>) will also be removed.
Default: __('Pages') - echo(boolean)
- Output the result to the screen (true) or return for processing (false).
Default: true - authors(string)
- Show pages belonging only to the authors specified in this parameter. You need to specify the author IDs, separated by commas.
Default: '' - sort_column(string)
Sort the list by the specified fields. By default, the list is sorted by titles (post_title) in alphabetical order. You can specify multiple parameters separated by commas, according to which the list will then be sorted.
post_title- sort by title (in alphabetical order);menu_order- sort by the order specified in the admin panel on the edit page of the "static page";post_date- sort by the creation date of the "static page";post_modified- sort by the modification date of the page;ID- sort by the record identifier in the Database (by ID);post_author- sort by author IDs;post_name- sort in alphabetical order by the alternative name of the post (usually transliteration of the title).
Default: 'menu_order, post_title'
- sort_order(string)
- Sorting direction: 'ASC' - in order, 'DESC' - in reverse order.
Default: 'ASC' - link_before(string)
- Specify here the text or HTML code that will be inserted before the link text (inside the
<a>tag). Added in version 2.7.
Default: "" - link_after(string)
- Specify here the text or HTML code that will be inserted after the link text (inside the
<a>tag). Added in version 2.7.
Default: "" - meta_key(string)
- Will output pages that have only the specified custom fields (works only with the meta_value parameter).
Default: '' - meta_value(string)
- Will output pages that have only the specified values of custom fields (the custom field key must be specified in the meta_key parameter).
Default: '' - number(int)
- Limit the number of links in the list (SQL LIMIT). In some cases, it may not work.
Default: 0 - offset(int)
- Top offset of the list. For example, if you specify 5, the first 5 links that should have been displayed will not be shown in the list.
Added in version 2.8.
Default: 0 - item_spacing(string)
- Whether to keep line breaks in the HTML code of the menu. Can be:
preserveordiscard. From WP 4.7.
Default: 'preserve' - walker(string)
- PHP class that handles the building of the list.
Default: ''
Examples
#1 Let's remove the title of the list by removing the title_li parameter.
Keep in mind that ul tags will also be deleted and must be specified separately:
<ul>
<?php wp_list_pages('title_li='); ?>
</ul> #2 Change the header
Change the title to "Poetry", wrap it in the HTML tag <h2> and list only the permanent pages with IDs 9, 5 and 23:
<ul>
<?php wp_list_pages( 'include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?>
</ul> #3 Sorting by ordinal number
Let's sort the list according to the ordinal numbers indicated on the "permanent pages" editing page:
<ul>
<?php wp_list_pages('sort_column=menu_order'); ?>
</ul> #4 Let's remove the "Pages" header from the list:
<ul>
<?php wp_list_pages('sort_column=menu_order&title_li='); ?>
</ul> #5 Sorting by date of creation
Let's display the list sorted by date of creation of the "permanent page" and show the date next to each link:
<ul>
<?php wp_list_pages('sort_column=post_date&show_date=created'); ?>
</ul> #6 Excluding pages
Using the parameter exclude we will exclude pages with IDs 17,38:
<ul>
<?php wp_list_pages('exclude=17,38' ); ?>
</ul> #7 List of only pages with the specified IDs
Using the parameter include, create a list of only pages with IDs 35, 7, 26 and 13:
<ul>
<?php wp_list_pages('include=7,13,26,35&title_li=<h2>' . __('Pages') . '</h2>' ); ?>
</ul> #8 Let's display the list of child pages
Using the following example, you can display in the sidebar (or elsewhere) a list of child pages (subpages) of the permanent page you are currently on. To do this, use parameter child_of and check if there are any child pages for the current page (let's try to get child pages into variable $children, and if it works, let's output a list):
<?php
$children = wp_list_pages( 'title_li=&child_of=' . $post->ID . '&echo=0' );
if( $children ){
?>
<ul>
<?php echo $children; ?>
</ul>
<?php
}
?> #9 Static list of child pages
Let's go further and display a list of child pages and make this list remain when we go to one of these child pages (i.e. for child pages, we get a list of single-level pages):
<?php
if( $post->post_parent ){
$children = wp_list_pages( "title_li=&child_of=" . $post->post_parent . "&echo=0" );
}
else {
$children = wp_list_pages( "title_li=&child_of=" . $post->ID . "&echo=0" );
}
if( $children ){
?>
<ul>
<?php echo $children; ?>
</ul>
<?php
}
?>
This example can be used in the sidebar (sidebar), but you should understand that it checks first if the current page has a parent page, if it exists then the list of single-level pages is displayed, if there is no parent page then the list of child pages is displayed. So we will have a closed list for the top-level pages on all pages.
#10 Static list of child pages (alternative)
An alternative version of the previous example.
This example can also be used in the sidebar, it will output:
-
when you are on the home page, all of the top-level "permanent pages" will be shown;
-
When you are on a top-level "permanent page" that has no child pages, all the same top-level "permanent pages" will be shown;
-
When you are on a top-level "permanent page" that has child pages, those child pages and their child pages will be shown;
- when you are on a child page of the top-level "permanent page" (the second-level page), the child pages of the top-level "permanent page" (i.e., the child pages of the parent page) will be shown.
$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; #11 List of pages, only if there is one
A list of child pages that will only be shown if the "permanent page" has child pages. The child pages will be shown both on the parent "permanent page" and on the child pages themselves. The difference from the previous example is that the name of the parent "permanent page" is in the header of the list:
<?php
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>
<ul>
<?php echo $children; ?>
</ul>
<?php
}
?> #12 All child pages on pages of any nesting level
This example shows how to get all child pages, on pages of any nesting level:
<?php
if( ! $post->post_parent ){
// get child pages, for the "permanent page" of the top level
$children = wp_list_pages( "title_li=&child_of=" . $post->ID . "&echo=0" );
}
else{
// get the child pages if we are on the first level child page.
//$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
if( $post->ancestors ){
// now get the ID of the very first page (the oldest one)
// wp collects IDs in reverse order, so the "first" page will be the last
$ancestors = end( $post->ancestors );
$children = wp_list_pages( "title_li=&child_of=" . $ancestors . "&echo=0" );
// that's it, now we will always have a list of all child pages,
// whatever nesting level we are on.
}
}
if( $children ){
?>
<ul>
<?php echo $children; ?>
</ul>
<?php
}
?> #13 The tree of child pages of the specified page
Since there is no way to tell wp_list_pages to output the tree (all nesting levels) of pages of a particular page (e.g. pages with ID 93), we can use get_pages() to do this trick:
<?php
// Use wp_list_pages to show the parent and all child pages
// (including all nested ones)
$pages = get_pages( 'child_of=93' );
$parent = 25;
if( $pages ){
$pageids = [];
foreach( $pages as $page ){
$pageids[] = $page->ID;
}
wp_list_pages( [
'title_li' => 'Parent page tree: ' . $parent,
'include' => $parent . ',' . implode( ',', $pageids ),
] );
}
?>
#14 Labeling and Styling page lists
By default, wp_list_pages() creates a list of this construction:
<li class="pagenav">Pages <ul> <li class="page_item page-item-4"></li> <li class="page_item page-item-5"></li> ... </ul> </li>
If we remove the title by putting an empty parameter title_li=, the view becomes like this:
<li class="page_item page-item-4"></li> <li class="page_item page-item-5"></li> ...
Another way of putting it is this:
<li class="pagenav"> Pages [title_li] <ul> <!-- The list starts here if the 'title_li' parameter is blank --> <li class="page-item-2 page_item current_page_ancestor current_page_parent"> [parent of the current page] <ul> <li class="page-item-21 page_item current_page_item">[current page]</li> </ul> </li> <li class="page-item-3 page_item">[other pages]</li> </ul> </li>
All li tags created with function wp_list_pages() are marked with CSS class page_item. When a page you are on appears in the list, another class current_page_parent is added to the li element of the list.
Thus, the list can be "colored" with the following CSS selectors:
.pagenav { ... } /* the main ul tag class that wraps the entire list */
.page-item-2 { ... } /* element related to page with ID 2 */
.page_item { ... } /* any list item */
.current_page_item { ... } /* current page */
.current_page_parent { ... } /* the parent page of the current page */
.current_page_ancestor { ... } /* any page either related to the current page (parent or daughter.) */
To achieve a global effect on list items, use the following selectors:
.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;
} #15 List of related or child pages to the current page
<?php
// if it is a child page
if( $post->post_parent ){
// collect related pages
$relations = get_post_ancestors( $post->ID );
// get child pages (if any)
$result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
// if the child pages of this page turned out to be
// get them merged with their relatives
foreach( $result as $page_id ){
$relations[] = $page_id->ID;
}
// add the current page to the related pages
$relations[] = $post->ID;
// make a comma-separated ID list of related, child and current
$relations_string = implode( ",", $relations );
// get the list using the include parameter
$sidelinks = wp_list_pages( "title_li=&echo=0&include=" . $relations_string );
}
// if it is not a child page
else {
// show only child pages one level
$sidelinks = wp_list_pages( "title_li=&echo=0&depth=1&child_of=" . $post->ID );
}
if( $sidelinks ){
?>
<h2><?php the_title(); ?></h2>
<ul>
<?php // display links in <li> tags
echo $sidelinks; ?>
</ul>
<?php
}
?> #16 Using an arbitrary record type (other than page)
If the site has an arbitrary record type with a tree structure, wp_list_pages() can be used to display such records as well.
In general, all of the above examples will work for this case as well, the only thing you need to change is the type of record the function will work with.
The record type is specified in the post_type parameter:
wp_list_pages( [
'post_type'=>'portfolio',
'title_li'=> __('Portfolio')
] );
Notes
- See: get_pages()
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 1.5.0 | Introduced. |
| Since 4.7.0 | Added the item_spacing argument. |