Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

if($post->post_parent)
{
    $children = wp_list_pages('title_li=&child_of=' . $post->post_parent . '&echo=0');
}
else
{
    $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?

Thanks!

share|improve this question
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
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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