Display Child Pages on parent page as menu items in wordpress

If you have some child pages for any parent page then you maybe want to add child pages in sidebar. Menu in website sidebar is common thing you have seen menu in sidebar in many websites. You can easily create a sidebar menu in wordpress using “wp-list-pages”. This blog post in not about “wp-list-pages”. I am assuming that you already know about this function. Today I will discuss about a specific scenario for your sidebar sub menu. The scenario is that you want to create a menu to display child pages. we can say that you have a main menu with its child menus. Now when a person clicks on that specific page all child pages of that page should appear in sidebar as sub menus. Same case happens when you click any of the child pages. All child pages should appear in the sidebar. If this is the problem you are facing right now that just paste this code in your sidebar and make CSS changes to display changes according to your requirements. You have to keep in mind that we are using “wp-list-pages” function for menu to display child pages.

  if($post->post_parent) {
  $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=0");
  $parenttitle = get_the_title($post->post_parent);
  else {
  $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->ID."&echo=0");
  $parenttitle = get_the_title($post->ID);
  if ($children) { ?>
    <h3> <?php echo $parenttitle; ?> Navigation </h3>
    <ul class="sub-menu">
        <?php echo $children; ?>
<?php } ?>

So that is pretty much all. hope you can get results according to your requirements. feel free to ask any question.

Leave a Reply