Wordpress : Display Always All Subpages in Sidebar


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



Copy this code and paste it in your HTML
  1. <?php // paste this in functions.php
  2. function wpe_highest_ancestor(){
  3. global $post;
  4. $page_ancestors = end($post->ancestors);
  5. if(!empty($page_ancestors)){
  6. $child_of = $page_ancestors;
  7. }else{
  8. $child_of = $post->ID;
  9. }
  10. return $child_of;
  11. }
  12. ?>
  13.  
  14. This is for the template
  15. -------------------------------
  16. <ul class="sidebar">
  17. <?php wp_list_pages('child_of=' . wpe_highest_ancestor()); ?>
  18. </ul>
  19.  
  20. If you want to have the name of the top-level page in the sidebar, you can use the following code:
  21. <ul class="sidebar">
  22. <?php
  23. $child_of = wpe_highest_ancestor();
  24. wp_list_pages('child_of='.$child_of.'&title_li=<h5>'.esc_attr(get_the_title($child_of)).'</h5>');
  25. ?>
  26. </ul>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.