Recently one of our users asked us how to display child pages of a WordPress Page? Often when working on a site that has pages with child pages, you may want to show those child pages on the parent page in a sidebar widget or another location in your template. In this article, we will show you how to display a list of child pages for a parent page in WordPress.
To see an example of a list of child pages on parent page, see the screenshot below that we have from OptinMonster’s How it Works page. You can also see this in use on WPBeginner’s Blueprint page.
Before we get start, for those who are not familiar with Child Pages, please check out our guide on the difference between Posts and Pages in WordPress. One of the important feature of pages is that they can be hierarchical. This means that a page can become a parent page and has child pages (i.e sub-pages) under it. This allows you to group different pages together under one parent page. For example, if you have a Product Page on a website, then you can add pages such as Features, Pricing, and Support as child pages. Each child page can have its own child pages as well.
Video Tutorial
If you don’t like the video or need more instructions, then continue reading.
To create a child page, simply create or edit a page in WordPress like you would normally do. Under the Page Attributes meta box, choose a parent page from the drop down menu.
Note: If you do not see the Page Attributes menu, then please click on the Screen Options button on the top right hand corner of your screen. It will display a menu where you need to make sure that Page Attributes is checked.
Displaying Child Pages on the Parent Page in WordPress
To list child pages under a parent page, you need to add the following code in a site-specific plugin, or in your theme’s functions.php file:
function wpb_list_child_pages() { global $post; if ( is_page() && $post->post_parent ) $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' ); else $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' ); if ( $childpages ) { $string = '<ul>' . $childpages . '</ul>'; } return $string; } add_shortcode('wpb_childpages', 'wpb_list_child_pages');
The code above first checks to see if a page has a parent or the page itself is a parent. If it is a parent page, then it displays the child pages associated with it. If it is a child page, then it displays all other child pages of its parent page. Lastly, if this is just a page with no child or parent page, then the code will simply do nothing. In the last line of the code, we have added a shortcode, so you can easily display child pages without modifying your page templates.
To display child pages simply add the following shortcode in a page or text widget in the sidebar:
[wpb_childpages]
In some cases, your theme may not be ready to execute shortcodes in a text widget. If it is not working, then see this tutorial on how to use shortcodes in WordPress sidebar widgets.
Dynamically Display Child Pages Without Any Shortcode
Using shortcode is convenient, but the problem with using shortcodes is that you will have to add shortcode in all pages that have parent or child pages. You may end up having shortcodes in lots of pages, and sometimes you may even forget to add the shortcode.
A better approach would be to edit the page template file in your theme, so that it can automatically display child pages. To do that you need to edit the main page.php
template or create a custom page template in your theme.
In your page template file, you need to add this line of code where you want to display child pages.
<?php wpb_list_child_pages(); ?>
That’s all. Your theme will now automatically detect child pages and display them.
If you are using parent pages with lots of child pages that have their own child pages, then the WordPress admin view can get confusing. For a better way to organize parent and pages try using admin column view.
We hope this article helped you list child pages in WordPress. Let us know if you have any questions or feedback by leaving a comment below.
Source: Thomas Griffin
I am using Generatepress Theme (created a child theme from it for customizing) and I can’t get this to work. The shortcode is displayed in the output page, it seems it is not even ercognized as being a shortcode. I use Elementor widgets and neither text nor shortcode widgets work.
Any idea of how to make it work when using elementor widgets?
Hi Boris,
Please try adding this code to your theme’s functions.php file or a site-specific plugin.
a great post, it’s exactly what I’m looking for since many hours ! and it works like a charm, just adding the code in the function.php of the theme then adding the short code in the page where I want the sub pages to appear, and voilà!!!
wonderful, thanks a lot for your generosity !!
Hi Frederic,
Glad you found it useful
You may also want to subscribe to our YouTube Channel for more WordPress video tutorials.
Hi!
Thanks for the code. How can I display the featured image of each child page?
Like with so many of the snippets we find, one has to go through all the comments in order to get it to work. What’s wrong with testing what you write?
Can you do a drop down menu for parent/child pages on wordpress.com, or does it have to be the .org version?
What if I want to display child posts, not child pages?
Using this code ends up displaying the parent page along with the child pages, which is redundant. As I understand it, what we really need is to display only the child pages, not the parent page. Any suggested modification that can do this?
Great, but it works not
add_shortcode is correct or must it be add_filter http://www.wpbeginner.com/wp-tutorials/how-to-use-shortcodes-in-your-wordpress-sidebar-widgets/ ?
with add_shortcode is error Fatal error: Uncaught Error: Call to undefined function add_shortcode()
with add_filter is no error but it works nort
On plugins update, for some strange reason I always get an error:
Fatal error: Cannot redeclare wpb_list_child_pages() (previously declared in …/wp-content/themes/pagelines/functions.php:25) in …/wp-content/themes/pagelines/functions.php on line 34
How can you also display the date of the child page?
Hello great article,
Can you please help me creating a dropdown of child pages in parent page.I need this functionality for one of my wordpress project and i am totally newbie to wordpress.
Am I the only one that cannot get it to work?
I have the following structure
About
— Page 1
— Page 2
— Page 3
When on the “About” or a child page (1, 2 or 3) I would like to have a list with my parent page (About) and all children (1, 2, 3) – Anyone got that working?
Thanks!
OMG sorry this is annoying Please ignore my other posts. Didn’t realize you couldn’t paste code into the comments.
I was NOT able to get it to work with wpb_list_child_pages();
I WAS able to get it to work with echo do_shortcode( ‘[wpb_childpages]’)
And to get the title of the parent page, i inserted this above the child page list:
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
PHP if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {echo get_the_title($grandparent); }else {echo get_the_title($parent);
I am using this code and it works great. Is it possible to also display categories in menus along with pages?
Hi,
I want to do the following
Parent page must be either for sale and or for rent
Then I want the child for example a province (gauteng) to link to the for sale and the for rent parent.
How do I do that?
Michelle
How can i create shortcode with param, for example, [wpb_childpages id=”1″], where id=”1″ is an id of parent page?
Hi,
Is it possible to limit the links to a specific number such as maximum 12 child pages?
Thanks
Thank you,
Could you explain, please, how can I organize child pages in drop down list that would be accessible through the parent page? (I don’t want the visitors could see all child pages in a form of a blogroll.)
I.e., on the parent page I want to create drop down list (listbox) to which child pages would be added in predefined order (say in alphabetical order). After reading Introduction a visitor can proceed by choosing any page from the list at her wish (child pages has no logical connection so in any case she will search the exact page).
Is ‘my dream’ realizable?
Thank you again.
How do I add pagination for Child Page?
Thanks!
Hi WPBeginner Staff,
I used the code you guys provided with the “short code option” and it worked (links to child pages). However, I wanted to use the permanent option, and that didn’t work.
When I added this line of code [ ] the parent page returns a 500 error and no child pages are displayed at atll.
What am I doing wrong?
On a side note if I wanted to display and excerpt with its respective image how would I go about it? Thanks!
I was able to figure this out.
I added the code snippet then added this shortcode [wpb_childpages] to the parent page where I wanted the child pages displayed. Awesome!
Now in order to display excerpts and an image would I use the same queries– just like posts under a category.
Thanks in advance
Hey I was wondering what you meant by ‘query’? How exactly did you get the excerpts and images to display in the parent page?
Thanks!
Really useful. Thank you.
Could someone confirm that the example page used in this tutorial http://optinmonster.com/how-it-works/ uses this feature or not? To me it looks like it they are using tabs, instead of child pages.
If I’m right, could someone direct me to a resource to get that feature, as I’m desperately looking for a fluid solution like the one they have in that page?
Many thanks,
Pradeep
For the non-shortcode option, you might want to let users know they need to either change
return $string;
toecho $string;
orin their page template echo out the function
echo page_list_child_pages();
Thanks to Erik for pointing this out above
Thank you!
Thank you! I was wondering why the code wouldn’t work…
Hi, I can get this to work when I am visiting my domain with a subdirectory (e.g – example.com/home) but when viewing the site without this (example.com) the list of sub pages is not listed. Can anyone point me as to where I am going wrong?
Thanks in advance – very new to WordPress.
How do you get this function to display the parent page itself at the top of the list along with the list of child and grandchild pages? On the child pages it does not list the parent page.
how can i customized this to just display the child page of a specific page?
How do u add a active class on the current ?
Is there now a plugin that can do same ?
and also can we decide where the box with submenus pages will go in any are of the page not just top of page? Using a plugin
Yes it can be used for custom post types. Like this:
function wpb_list_child_pages() {
global $post;
if ( 'movie' == get_post_type() && $post->post_parent )
$childpages = wp_list_pages( 'post_type=movie&sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
$childpages = wp_list_pages( 'post_type=movie&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
$string = '' . $childpages . '';
}
return $string;
}
add_shortcode('wpb_childpages', 'wpb_list_child_pages');
This is great! Is there a way to do the same for Custom Post Types that have child posts?
Also wondering!
If anyone knows, please help!
Is there a way to modify this in order to print custom post type child posts on a custom post type post page? Thanks, it is very handy!
It’s very greate for me! Anyways I would like to know how can I display title and thumbnail of child page to parent page.
Hi, I have the same query. I really like how the list is styled on the Beginners Blueprint page but I have no idea how to start. Can someone recommend an article to read or give some advice, thanks.
Hello!
Nice snippet, but how do I list only grandchild pages?
Thank you!
There is an error in the pages which are not having child pages so I have slightly modified
if( count($childpages) != 0 ) {
$string = ” . $childpages . ”;
}
instead of
if( $childpages ){
$string = ” . $childpages . ”;
}
In order to execute a shortcode in WordPress templates you need to add it like this:
I couldn’t get the template code “wpb_list_child_pages();” to work, but echo shortcode did the trick. Cheers!
Just change the “return $string” to “echo $string” (this will print out the pages).
thanks, it worked
I’ve added the code to the functions.php and when i add the [wpb_childpages] shortcode in side my text it works. But when try to add to one of my templates it doesn’t show a thing. Anybody knows what i’m doing wrong?
Great explanation!!
Is there a way to display always the parent pages?
This is brilliant, thanks.
Is there a way to adapt it so the Parent displays the Grandchild, and not the Child?
Great function , thanks !
The great idea, thanks dude.
Can you tell us how to stop the list at the first level? I’d like to show all the subpages of that parent, but not the subpages’ children. Thanks.
Just add &depth=1
sort_column=menu_order&title_li=&child_of=’ . $post->post_parent . ‘&echo=0&depth=1
Great. I love this idea of a site plugin and seeing the code you are adding (so you know it is clean and doesn’t do bad stuff) rather than relying on some plugin that could be installing anything.
For something as simple as listing child pages I agree it should be part of a larger plugin rather than just one plugin otherwise you end up with so many plugins it is hard to keep track.
This is great. I had a page on my website called “Services” and was manually linking the sub-pages, but not any more. Awesome. Thanks for this tip. Worked like a charm and did exactly what I was doing manually.
This is great! Putting it in a plugin is great feature and great opportunity to extend functionality by adding:
– child page featured image (as thumbnail)
– child Title
– rest of the meta (author, date, comment count)
– styling in columns maybe
and so on
This way, we would have almost complete Portfolio plugin – portfolios made of Pages rather than just made of custom post types.
Thank you..
The Best plugin for displaying child pages on a sidebar for those who want even faster and “cleaner” child-pages-navigation:
http://wordpress.org/plugins/child-page-navigation/
hi there
thanks it was very useful . but if you could show it by video it was very more useable.as tyler moor would do
thanks
yousef
This is great! I have a lot of manually added links to child pages, and this is going to make maintaining our site MUCH easier. I’ve just been testing in my development environment, and I’d like to tweak the function a little bit, so that on a child page, when the shortcode is included, all of the children of the same parent are listed EXCEPT for the currently displayed page. I’m really still learning PHP, so while I’ll be trying to figure this one out on my own, I’d welcome any assistance in making that change to the code.