Recently our one customer asked us how to show child pages of a WordPress Page? Often when operating on a website that has pages with child pages, you may desire to reveal those child pages on the parent page in a sidebar widget or one more location in your template or pattern. Today, we will show you how to show a list of child pages for a parent page in WordPress.
Before starting, for those who are not recognizable with Child Pages, please make sure out our guide on the difference between pages & posts in WordPress. One of the vital features of pages is that they can be hierarchical. It means that a page can become a parent page & has child pages (i.e sub-pages) under it. This permits you to group various pages together under one parent page. For instance, if you have a Product Page on a website, then you can put in more pages such as aspects, Pricing, & Support as child pages. Every child page can have its own child pages also.
To make a child page, only generate or edit a page in WordPress like you would usually do. Under the Page Attributes Meta box, select a parent page from the drop down menu. If you will not see the Page Attributes menu, then click on the Screen Options button on the top right hand corner of your screen. It will show a menu where you require making sure that Page Attributes is checked.
Showing Child Pages on the Parent Page in WordPress
To list child pages under a parent page, you require adding the following code in a site-specific plug-in, or in your theme’s functions.php file:
The code above initial checks to watch if a page has a parent or the page itself is a parent. If this is a parent page, then it shows the child pages connected with it. If this is a child page, then it reveals all other child pages of its parent page. Finally, if it is just a page with no child or parent page, then the code will just do nothing. In the last line of the code, we have added a shortcode, so, you can simply show child pages without changing your page templates.
To show child pages only add the following shortcode in a page or text widget in the sidebar:
Sometimes, your theme may not be prepared to execute shortcodes in a text widget. If this is not working, then see this tutorial on how to utilize shortcodes in WordPress sidebar widgets.
Dynamically Show Child Pages Without Any Short code
The use of short code is right, but the trouble with the use of short codes is that you have to add short code on all pages that have parents or children pages. You can have short codes in many pages, & sometimes even forget you can add the short code. A enhanced approach is to edit the template file page on the subject, so that it can automatically display the child pages. To do that you require editing the main page.php template or make a custom page template in your theme. In the page template file, you must add this line of code where you want to show child pages.
That is all. Your theme will now automatically detect child pages & show them. If you are utilizing parent pages with many child pages that have their own child pages, then the WordPress admin view can find puzzling. For an improved method to organize parent & pages try utilizing admin column view.
About the Editorial Staff