How to Display a List of Child Pages For a Parent Page in WordPress

When You Need to Show a List of Child Pages?

WordPress comes with two default post types calledΒ posts and pages. Posts are blog content, and they are usually organized withΒ categories and tags.

Pages are one-off or standalone content that are evergreen such as β€˜About us’ page or β€˜Contact us’ page.

In WordPress, pages can be hierarchical which means you can organize them with parent and child pages.

For instance, you may want to create a product page with child pages for Features, Pricing, and Support.

To create a child page, follow our guide onΒ how to create a child page in WordPress.

After you have created your parent and child pages, you may want to list child pages on the main parent page.

Now an easy way to do this is by manually editing the parent page and add a list of links individually.

However, you’ll need to manually edit the parent page each time you add or delete a child page. Wouldn’t it be nicer if you could just create a child page and it would automatically appear as a link on the parent page?

That being said, let’s take a look at some other dynamic ways to quickly display a list of child pages on the parent page in WordPress.

Method 1. Display Child Pages on Parent Page using a Plugin

This method is easier and recommended for all users.

First, you need to install and activate theΒ Page-listΒ plugin. For more details, see our step by step guide onΒ how to install a WordPress plugin.

Upon activation, you need to edit the parent page and simply add the followingΒ shortcodeΒ where you want to display the list of child pages.

You can now save your page and preview it in a new browser tab. You’ll notice that it displays a simple bulleted list of all the child pages.

If you want, you can add someΒ custom CSSΒ to change the appearance of the list. Here is some sample CSS you can use as a starting point.

1 ul.page-list.subpages-page-list {
2 list-style: none;
3 Β list-style-type: none;
4 Β background-color: #eee;
5 border: 1px solid #CCC;
6 padding: 20px;
7 }

After applying your custom CSS you can preview the parent page. This is how it looked on our testΒ WordPress website.

The plugin provides a bunch of shortcode parameters that allow you to set depth, exclude pages, number of items, and more. For details, pleaseΒ see the plugin’s pageΒ for detailed documentation.

Method 2. List Child Pages for a Parent Page using Code

This method is a bit advanced and requires to you add code to your WordPress website. If you have not done this before, then please take a look at our guide onΒ how to copy and paste code 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 class=”wpb_page_list”>’ . $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:

Don’t forget to save your changes and preview them in a browser tab. This is how it appears on our test site.

You can now style this page list using some custom CSS. Here is some sample CSS code you can use as a starting point.

ul.wpb_page_list {
Β Β Β Β list-style: none;
Β Β Β Β list-style-type: none;
Β Β Β Β background-color: #eee;
Β Β Β Β border: 1px solid #CCC;
Β Β Β Β padding: 20px;
}

Method 3. Dynamically Display Child Pages Without Any Shortcode

Using shortcodes is convenient, but the problem with them is that you will have to add shortcodes 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 it.

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 Β template, orΒ create a custom page templateΒ in your theme.

You can edit your main theme, but those changes will disappear if you change or update your theme. That’s why it would be better if youΒ create a child themeΒ and then make your changes in the child theme.

In your page template file, you need to add this line of code where you want to display child pages.

That’s all. Your theme will now automatically detect child pages and display them in a plain list.

You can customize the styles with CSS and formatting. Here’s an example of howΒ OptinMonster websiteΒ shows the parent page and sub pages:

We hope this article helped you list child pages for a parent page in WordPress. You may also want to see our guide onΒ the most important pages to createΒ on a new WordPress website, and our comparison of theΒ best drag & drop WordPress page buildersΒ to create custom layouts without any code.

If you liked this article, then please subscribe to ourΒ YouTube ChannelΒ for WordPress video tutorials. You can also find us onΒ TwitterΒ andΒ Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!

Pin It on Pinterest

Add address