It's possible that I'm just doing this wrong, so feel free to let me know how you would solve this problem.

I have a mostly page-based site, but there are a few "pages" where my content is coming from a custom query_posts() call. In all of these pages I want to be able to use wp_list_pages() to generate sub-navigation. This works just fine when I'm in a normal page, but when I'm in a "page" with query_posts() content, this doesn't work at all.

Here's an example of what I mean by a "page". This one is a theme file called page-media.php:

<?php get_header(); ?>

<?php query_posts('category_name=in-the-media'); ?>

    <?php get_template_part('loop', 'index'); ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

The sub-navigation happens in sidebar.php, so it's happening post-loop.

Here's how I'm generating my menu:

    $children = wp_list_pages('title_li=&child_of=' . $post->post_parent . '&echo=0');
    $children = wp_list_pages('title_li=&child_of=' . $post->ID . '&echo=0');

I then print $children later in a <ul>. This works perfectly in a page or its sub-page, but displays nothing in a page where I've called query_posts().

Am I approaching this correctly? Is there something I'm missing?


  • 2
    Add wp_reset_query(); after the loop before wp_list_pages – Bainternet Mar 18 '11 at 19:56
  • Oh! Sorry, that did the trick! You should write that as an answer so I can accept it. Thanks! – Gavin Anderegg Mar 18 '11 at 20:12

This is for future askers,
Whenever you use query_posts or custom wp_query you should add wp_reset_query(); after your loop or when you are done processing your posts. This little line of code will help in solving pagination issues or in your case rest the global $post the original as before your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.