Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top