- Web templates
- E-commerce Templates
- CMS & Blog Templates
- Facebook Templates
- Website Builders
WordPress. How to display a list of child pages for a parent page
March 31, 2015
Today you will learn how to display a list of child pages for a parent page in WordPress.
WordPress. How to display a list of child pages for a parent page
You should follow the steps below:
-
Enter your cpanel/ftp and then follow this path: wp-content/themes/themeXXXXX/includes (you can also do that through admin panel, you need to open following menu: Appearence -> Editor), there you will find custom-function.php file where you need to add the following code:
function my_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('my_childpages', 'my_list_child_pages');
-
The above code first checks 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:
[my_childpages]
After re-loading the page you will be able to see the list of child pages. In case you need the detailed information, feel free to check the video version.
WordPress. How to display a list of child pages for a parent page