I am trying to provide a WordPress website for a small commercial customer. I would like their site to display a navigation menu where all pages are displayed hierarchically. To this effect, I am attempting to use nested WP_Query's; one top-level query to get the top-level pages, and second-level queries to get the children of a particular top-level page. My code is:
$pages = new WP_Query(array('post_type' => 'page'));
$pages->the_post(); // advance the query enumerator
$page = $post;
if (!($page->post_parent)) // Only act on top-level pages
<!-- HTML for parent -->
$children = new WP_Query(array('post_type' => 'page', 'post_parent' => $page->ID));
$children->the_post(); // advance the query enumerator
$child = $post;
<!-- HTML for child -->
wp_reset_postdata(); // Reset the_loop since our calls to WP_Query->the_post() overwrote the global the_loop
The code iterates once through the outer while-loop (incidentally it picks up the "Home" page, which has no children, so the inner loop is not executed) and then when it begins a second iteration of the outer loop, I get this error:
Fatal error: Call to a member function have_posts() on a non-object in /Users/me/Sites/customer/wordpress/wp-content/themes/customer/index.php on line XX
Line XX corresponds to
while ($pages->have_posts()) in my code.
1) is there an easier way to get a hierarchy of the pages? 2) What am I doing wrong with my queries/the_loop? Thank you very much.