WordPress: Custom Sub Navigation

/ Published in: PHP
Save to your folder(s)

This code will list all children pages from a parent page. This method is useful when you don\\\'t want to list all parent pages when you are on a parent page that have no children pages.

Copy this code and paste it in your HTML
  1. <?php
  2. global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to $thispage
  3. $subpages = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of pages that are sub pages of $thispage and assigns it to $pagekids
  4. ?>
  6. <?php if ($subpages) { // if there are any values stored in $pagekids, meaning there are sub-pages of the current page ?>
  8. <ul class="links">
  9. <?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display ONLY the sub pages of the current page ?>
  10. </ul>
  12. <?php } else { // $pagekids is empty ?>
  14. <ul class="links">
  15. <?php wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&sort_column=menu_order'); // display the sub-pages of the current parent page ?>
  16. </ul>
  18. <?php } ?>

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.