If you dont mind using a plugin I can tell you how I have gotten this to work in the past.
- Add the following code to your functions.php
function get_root_parent($page_id) { global $wpdb; $parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type="page" AND ID = '$page_id'"); if ($parent == 0) return $page_id; else return get_root_parent($parent); }
- Add the following to header.php between the close of head and open of body in a php wrapper
<?php $GLOBALS["parentId"] = get_root_parent($post->ID); ?>
- Download and install the fold pages plugin: http://www.webspaceworks.com/resources/wordpress/30/
- Add the following code to your sidebar.php
<ul> <?php $thispage = $wp_query->post; if ($thispage->post_parent!=0) { wswwpx_fold_page_list("sort_column=menu_order&child_of=".$GLOBALS["parentId"]."&title_li=&child_of=".$thispage->post_parent); } else { wswwpx_fold_page_list("sort_column=menu_order&child_of=".$GLOBALS["parentId"]."&title_li=&child_of=".$thispage->ID); }?> </ul>