Show Top Level and Child Pages in Wordpress


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

use this in the sidebar to show a top level page with child pages persistently


Copy this code and paste it in your HTML
  1. <ul><?php $parent_title = get_the_title($post->post_parent);?>
  2. <li><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li></ul>
  3. <?php
  4.  
  5. if ($post->post_parent) {
  6. $ancestors=get_post_ancestors($post->ID);
  7. $root=count($ancestors)-1;
  8. $parent = $ancestors[$root];
  9. } else {
  10. $parent = $post->ID;
  11. }
  12.  
  13. $children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");
  14.  
  15. if ($children) { ?>
  16. <ul id="subnav">
  17. <?php echo $children; ?>
  18. </ul>
  19. <?php } ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.