Display subpages when parent-/subpage is opened in Wordpress


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



Copy this code and paste it in your HTML
  1. function navigation() {
  2. global $wpdb, $post;
  3.  
  4. $query = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = 0");
  5.  
  6. echo '<div id="nav">';
  7. echo '<ul>';
  8.  
  9. foreach ($query as $obj) {
  10.  
  11. echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$obj->ID.'">'.$obj->post_title.'</a></li>';
  12.  
  13. $sub_query = $wpdb->get_results("SELECT ID, post_title, post_parent FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'page' AND post_parent = {$obj->ID}");
  14.  
  15. foreach($sub_query as $sub_obj) {
  16. if($post->ID == $sub_obj->post_parent)
  17. echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  18.  
  19. if($post->post_parent == $sub_obj->post_parent)
  20. echo '<li><a href="'.get_bloginfo('wpurl').'/?page_id='.$sub_obj->ID.'" class="subpage">'.$sub_obj->post_title.'</a></li>';
  21. }
  22.  
  23.  
  24. }
  25.  
  26. echo '</ul>';
  27. echo '</div>';
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.