• Resolved Steven Jones

    (@rainemaida)


    Ok,

    On my newssidebar.php I have:

    <?php query_posts('cat=4'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                                <li><a href="#"><?php the_title(); ?></a></li>
                                <?php endwhile; else: ?>
    <?php endif; ?>

    This brings a list back of my posts in category 4 which is news.

    On single.php I have this code:

    <?php get_header(); ?>
    <div id="body">
    
    <?php
      $post = $wp_query->post;
    
      if ( in_category('1') ) {
      include (TEMPLATEPATH . '/blogsidebar.php');
      } 
    
      elseif ( in_category('4') ) {
      include (TEMPLATEPATH . '/newssidebar.php');
      } 
    
      else {
      include (TEMPLATEPATH . '/sidebar.php');
      }
    ?>
    
                    	<div id="right">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h2><?php the_title() ?></h2>
    <p><?php the_content(); ?></p>
    <?php previous_post_link('&laquo; %link &laquo;') ?> <br />
    		 <?php next_post_link('&raquo; %link &raquo;') ?>
    
    <?php next_posts_link('&laquo; Older Entries') ?> <?php previous_posts_link('Newer Entries &raquo;') ?>
    
    <?php endwhile; else: ?>
    
    <?php endif; ?>
    
                        </div>
                    <!--right end -->
                <br class="spacer" />
                </div>
    <?php get_footer(); ?>

    So this brings back newssidebar.php – just how I want it to.

    However instead of getting a single posts it posts everything in category 4 in the single.php which makes me think that the loop in newssidebar.php hasn’t been closed correctly.

    Can someone please help out.

    Cheers,
    Steve

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Steven Jones

    (@rainemaida)

    Does anyone have any suggestions?

    You’re right. See the multiple loop examples at http://codex.wordpress.org/The_Loop for how to fix your sidebar code. Right at the end of “multiple loops example 1”. You need a totally different query when writing a sidebar, because you don’t know whether the sidebar code will be called before the main loop or after.

    Try this (replace query contents and “Do stuff” as appropriate):

    <?php $my_query = new WP_Query('category_name=news');
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <!-- Do stuff... -->
    <?php endwhile; ?>

    By storing the (new) query in a new instance, you should not be affecting the active query set up by WordPress.

    You may need to add this function inside your main Loop, right after the_post():
    update_post_caches($posts);

    I noticed each time I look at the Loop documentation it has more and more examples. It’s great documentation; unfortunately it won’t cover every instance but if you read through it it will show you the best way to do things.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Loop won’t close so I can open another.’ is closed to new replies.