WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query breaking conditional children list (5 posts)

  1. bacollier
    Member
    Posted 5 years ago #

    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

  2. bacollier
    Member
    Posted 5 years ago #

    anyone?

  3. Mayur Jobanputra
    Member
    Posted 4 years ago #

    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(); ?>

  4. <div class="info">
    " rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?>
    </div>
    <div class="clear"></div>
  5. <?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; ?>
  6. Mayur Jobanputra
    Member
    Posted 4 years ago #

    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

  7. trystaltech
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic