WordPress.org

Support

Support » How-To and Troubleshooting » WP_Query breaking conditional children list

WP_Query breaking conditional children list

  • bacollier
    Member

    @bacollier

    Hey There!

    I’ve been roaming the forums for a few hours now trying to dig out an answer but I can’t seem to find one. I have a page template querying a certain category of posts (under the page’s contents) and it is breaking my sidebar children navigation.

    I’ve read on the forums that I should be using WP_Query but this still breaks the conditional element in the children/parent sidebar nav.

    Here’s the query code that I’m using:

    <?php
    $otherPosts = new WP_Query();
    $otherPosts->query('cat=3');
    while($otherPosts->have_posts()) : $otherPosts->the_post();
    ?>
    
    <h3><?php the_title(); ?></h3>
    <?php the_content();?>
    
    <?php endwhile; ?>

    and here is the sidebar code that is getting broken:

    <?php
    if($post->post_parent)
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul class="children">
    <?php echo '<h3>Pages</h3>' ?>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    I’ve also tried the query reset code but that doesn’t help – if anyone has any pointers I’d be so grateful!

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • bacollier
    Member

    @bacollier

    anyone?

    I’m having the exact same problem and haven’t figure id out either! In my header I have:

    <?php $my_query = new WP_Query(‘category_name=news’);
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

  • <div class=”info”>
    ” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?>
    </div>
    <div class=”clear”></div>
  • <?php endwhile; ?>

    and in my sidebar, I have

    echo “<h1>”;wp_title(”);echo “</h1>”;
    if($post->post_parent)
    $children = wp_list_pages(“sort_column=menu_order&depth=1&title_li=&child_of=”.$post->post_parent.”&echo=0″); else $children = wp_list_pages(“sort_column=menu_order&depth=1&title_li=&child_of=”.$post->ID.”&echo=0″);
    if ($children) { ?>

      <?php echo $children; ?>

    GOT IT WORKING!! Yay! After 2+ hours of combing through dozens of examples. Here is the trick. In your first code block, you need to issue the following command once it’s completed.

    wp_reset_query();

    That made both my header news ticker work with listing blog posts and my sidebar child page listing work. I snazzed it up a bit more by adding permalinks, and always showing the title of the parent page on child or parent pages. Here is my working code.

    This is in my header, outside of a page loop and near my logo. It shows all posts from my news category so I can use it in a jquery ticker thingmabob

    And this code block sits in my sidebar.php. It lists child pages. I made a few enhancements so that it always shows the title of the parent page (in my case my root level pages) regardless of if you are on child pages or not. In addition, I hyperlinked the parent page title (h1) so that on child pages, you can navigate back to the parent if you like.

    <?php
    if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    $parent_url=get_permalink($post->post_parent);
    echo “<h1>“.$parent_title.”</h1>”;
    } else {
    $datitle=wp_title(”,0);
    echo “<h1>”.$datitle.”</h1>”;
    }
    ?>

    <?php
    if($post->post_parent)
    $children = wp_list_pages(“sort_column=menu_order&depth=1&title_li=&child_of=”.$post->post_parent.”&echo=0″); else $children = wp_list_pages(“sort_column=menu_order&depth=1&title_li=&child_of=”.$post->ID.”&echo=0″);
    if ($children) { ?>

      <?php echo $children; ?>

    <?php } ?>

    To see the working version on my site, visit http://www.fullmotiongroup.com/about

    trystaltech
    Member

    @trystaltech

    genius, wp_reset_query(); works perfectly, thankyou very much 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query breaking conditional children list’ is closed to new replies.