The opportunity to list child pages for specific parent pages in WordPress came up for me the other day.
It took me a few tries to get it right, but I believe I have come up with a nice working solution to list child pages in your sidebar and/or pages themselves. It is also dynamic, meaning that it will work for any page that has child pages. I will give you a few examples for implementing this into your theme, including examples for template files, Genesis/Thesis and sidebar widgets. Let’s get started!
Listing Child Pages in Your Template Files and Sidebar Widgets
This one is fairly easy to do. All you need to do is open up the template file where you would want the child navigation to appear (most likely the
page.php or the
sidebar.php file) or a widget that can parse PHP (WP PHP Widget works just fine) and place it in your desired spot. Copy and paste the code below into the file or widget (sans the opening PHP tag):
Now save your file or widget and reload your page. You will now notice that any page that does not have child pages will have nothing displayed, and any page that does have child pages will have them displayed. Also, this code makes it so that it still appears even if you are on a child page of the specified parent page!
wp_list_pagesfunction. Check out this page to learn more!
Listing Child Pages in Genesis
If you are a Genesis user, simply copy and paste the code below into the appropriate child theme’s
functions.php file (sans the opening PHP tag):
Genesis offers a broad range of hooks for you to use. You can learn more about those hooks by viewing the Genesis Hook Reference page.
And that’s it folks! I hope you enjoyed the tutorial and have found it useful. If you have any questions about how to list child pages in your WordPress theme, just let me know in the comments and I will do my best to help you out!