How to create a dynamic list of page title links when you are on a page that has sub pages, ie: If using a gallery for a page, were each sub-page is a gallery of images
When you have multiple pages in your wordpress website, either using it as a blog or as a complete CMS, this quick tip will help show you how to:
Only when you are on that Parent page!
Below is the HTML framework for our sidebar navigation…
- Gallery
- page 1
- page 2
- page 3
- page 4
- page 5
<ul>
<li>Gallery
<ul>
<li>page 1</li>
<li>page 2</li>
<li>page 3</li>
<li>page 4</li>
<li>page 5</li>
</ul>
</li>
</ul>
When on the “PARENT” page,
The code in the sidebar, navigation menu or page,
will be populated with a list of Child Sub pages for THAT current PARENT page only…
Copy the code below into your sidebar, and try it…
<?php if ( is_page() ) {
if($post->post_parent)
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
else
$children = wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
?>
<div class="sidebar">
<h2>Sub-pages of Current Page</h2>
<ul>
<?php echo $children; ?>
</ul>
</div>
<?php
} // End If Post
} // End if is page
?>
As long as your Page has at least one associated child page, the list will be shown, if there are no child pages then the menu will not be shown.. this is handy if you’re sidebar is quite packed with elements/ads, this will be generated only on that parent page…
you can check out the example on the Shugtech Gallery website.
Each Gallery Album is a child of Gallery, so each image gallery is a separate wp page were the list of other Albums available is on the right hand sidebar…
As always feedback is welcome..
Latest posts by Marty (see all)
- MTB Cononish Gold Mine Near Tyndrum - June 17, 2013
- Know the Difference Between the Most Common Arduino Boards - June 14, 2013
- How To: Connect Your Old SD Card To An Arduino - June 7, 2013

at 1:32 pm
I think what your after is more to do with the wp_nav_menu rather than wp_list_pages()
ie:
wp_nav_menu( array( ‘sort_column’ => ‘menu_order’, ‘container_class’ => ‘menu-header’ ) );
the sort column is set to menu_order which will take the ordering of the draggable menu, but this is used inside wp_nav_menu
see this page: http://codex.wordpress.org/Function_Reference/wp_nav_menu
at 1:11 pm
I see it. However, I would be able to enter the appearance in the main menu and select options menu, to be able to drag and drop the pages of the menu.
at 1:05 pm
Hi Lars, the code is already in there,,
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$post->post_parent.’&echo=0′);
you set the order of pages, when creating the page. using a number for each!
ie: homepage = 1, about us = 2, contact us = 3, this will be used when populating the wp_list_pages array.
then you can use the sort=ASC, or DESC to list by Highest to lowest or vice versa..
at 12:45 pm
Hi Marty,
Have a look at that link, but I do not really understand how I applisera it to the script above. Someone who wants to cook it in the script above and post here?
at 12:40 pm
Hi Lars,
you should be able to use the ‘sort_column=menu_order’
please see http://codex.wordpress.org/Function_Reference/wp_list_pages
at 12:01 pm
Hello,
Good script, I Avander below skrip and have a small question.
How do I do if I want the child pages to be sorted by the order that I put in the menu on appearance?
post_parent)
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$post->post_parent.’&echo=0′);
else
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$post->ID.’&echo=0′);
if ($children) {
at 3:12 pm
You can also use the http://wordpress.org/extend/plugins/hierarchical-pages/ Hierarchical Pages widget which lets you list only children of the current page… and optionally all the top-level pages, plus a variety of other options. It also includes a Hierarchical Taxonomy widget that works for built-in Categories and user-defined taxonomies.
at 1:51 pm
ups corrected code:
if ( is_page() ) {
global $wp_query;
if($wp_query->post->post_parent) {
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$wp_query->post->post_parent.’&echo=1′);
}
else {
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$wp_query->post->ID.’&echo=1′);
}
if ($children) {
<?php
} // End If Post
} // End if is page
at 12:46 pm
I got the same problem as Bob and it was cause its in the sidebar…
This solution works for me:
post->post_parent) {
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$wp_query->post->post_parent.’&echo=1′);
}
else {
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$wp_query->post->ID.’&echo=1′);
}
/*
$page_id = $wp_query->post->ID;
$children = wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’.$page_id.’&echo=0′);
*/
if ($children) {
at 1:26 pm
Hi Martin, I tried in the header file initially but didn’t seem to work. I’m using quite a complicated theme which would be why it won’t work. I’ll email you the site details throgh the contact form if that’s ok?