WordPress sub page and parent page menu


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

Shows the sub pages of a parent page, and also the parent page as a link.


Copy this code and paste it in your HTML
  1. <?php
  2. if(!$post->post_parent){
  3. // will display the subpages of this top level page
  4. $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  5. }else{
  6. // diplays only the subpages of parent level
  7. //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  8.  
  9. if($post->ancestors)
  10. {
  11. // now you can get the the top ID of this page
  12. // wp is putting the ids DESC, thats why the top level ID is the last one
  13. $ancestors = end($post->ancestors);
  14. $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
  15. // you will always get the whole subpages list
  16. }
  17. }
  18. if ($children) { ?>
  19. <ul>
  20. <?php
  21. $parent_link = get_permalink($post->post_parent);
  22. $parent_title = get_the_title($post->post_parent);
  23. if ($post->post_parent) {
  24. echo "<li><a href='".$parent_link."'>".$parent_title."</a></li>";
  25. } else {
  26. echo "<li class='current_page_item'><a href='".$parent_link."'>".$parent_title."</a></li>";
  27. }
  28. ?>
  29. <?php echo $children; ?>
  30. </ul>
  31. <?php } ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.