I’m using the wp_list_pages function a lot for displaying parent pages in the main menu.
I usually only show the parent pages without children (on a homepage). In the function I use depth=1 to only sum up the top level pages:
wp_list_pages('title_li=&depth=1' ); |
When visiting a page i like to sum up the subpages of the parent page in a submenu. Sometimes (when using a submenu on a left sidebar) i like to show the parent page again on the top of the subitems. I created a function for this which you can put in your functions.php template file. This function is only created to list 1 level of subpages, no sub-sub is supported (yet) on the moment. Maybe I will update the function when I need it myself
The function in you functions.php looks like this
function wp_list_subpages($list_parent_page = true){ global $post; $parent_id = $post->post_parent; $parent_link = array(); // if we are on a subpage if ($parent_id) { while ($parent_id) { $page = get_page($parent_id); $parent_link[] = '<li><a href="'.get_permalink($page->ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'</a></li>'; $parent_id = $page->ID; // the parent id we need to list subpages from break; // stop after 1 } // if we are on the parent page } else { $parent_id = $post->ID; // the parent id we need to list subpages from $parent_link[] = '<li class="current_page_item"><a href="'.get_permalink($page->ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'</a></li>'; } // default = true if($list_parent_page == true){ echo $parent_link[0]; } // list the subpages $subpages = wp_list_pages('title_li=&depth=1&echo=0&child_of=' . $parent_id); if ($subpages) { echo $subpages; } } |
On your template you can use the following function to show subpages (including the parent page):
wp_list_subpages(); |
If you don’t want to list the parent before the subpages, use:
wp_list_subpages(false); |
All comments or improvents are welcome! This is just something i really use a lot and hopefully someone is happy with it.