WordPress.org

Support

Support » How-To and Troubleshooting » Second Loop in Sidebar of Twenty Ten Child Theme

Second Loop in Sidebar of Twenty Ten Child Theme

  • I’m working on a child theme of Twenty Ten where I want to run a second loop in the sidebar for single posts and categories that will list recent posts minus the post(s) already shown on the page.

    I am storing the ID’s of the original posts in an array named $ids, then excluding those ID’s in the second loop.

    Here’s the weird part. If I add the second loop after the first in loop.php, it works as intended. But if I add the second loop to sidebar.php, it fails to exclude the original posts. Some testing reveals that the variable $ids loses it’s content going from the loop.php to sidebar.php. Here’s the code, with all of the extra stuff stripped out:

    <?php 	$ids = array();
    	while (have_posts()) : the_post();
    	$ids[] = get_the_ID();
     ?>
    
    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    
    <?php endwhile; // End the loop. Whew. ?>
    
    <!-- print out the Post ID, category, and what's stored in the variable $ids -->
        <p>Result of print_r($ids): <?php print_r($ids) ?></p>
        <hr />
    <!-- Second loop ********************************  -->
    
    <?php // Create a new instance
    $second_query = new WP_Query(array('post__not_in' => $ids));
    // The Loop
    while( $second_query->have_posts() ) : $second_query->the_post();
     echo '<li>';
     the_title();
     echo '</li>';
    endwhile;
    wp_reset_query();
     ?>    	
    
    <!-- End of Second Loop ******************  -->

    So far, this does what I want it to. To get the same result in the sidebar, I’m using:

    <!-- print out the Post ID, category, and what's stored in the variable $ids -->
        <p>Result of print_r($ids): <?php print_r($ids) ?></p>	
    
    <?php // Create a new instance
    	$third_query = new WP_Query(array('post__not_in' => $ids));
    	// The Loop
    	while( $third_query->have_posts() ) : $third_query->the_post(); ?>
    <?php
    	echo '<li>';
     	the_title();
     	echo '</li>';
    	endwhile;
    	wp_reset_query();
     ?>

    For some reason, it fails. The output of that print_r($ids) command at the end of the loop.php is the array of post-IDs, as it should be. The output of print_r($ids) at the top of sidebar.php is empty, thus the third_query fails to exclude those posts.

    Any suggestions?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Make $ids global in the sidebar:

    <!-- print out the Post ID, category, and what's stored in the variable $ids -->
       <?php global $ids; ?>
        <p>Result of print_r($ids): <?php print_r($ids) ?></p>

    And, you should call it something more unique than $ids, just in case someone else uses that.

    I changed the variable $ids to $do_not_duplicate everywhere it was in the code, and added the suggested code to the sidebar.php as follows:

    <!-- print out the Post ID, category, and what's stored in the variable $do_not_duplicate -->
    <?php global $do_not_duplicate; ?>
    <p>Result of print_r($do_not_duplicate): <?php print_r($do_not_duplicate) ?></p>
    
    <?php // Create a new instance
    	$third_query = new WP_Query(array('post__not_in' => $do_not_duplicate));
    	// The Loop
    	while( $third_query->have_posts() ) : $third_query->the_post(); ?>
    <?php
    	echo '<li>';
     	the_title();
     	echo '</li>';
    	endwhile;
    	wp_reset_query();
     ?>

    I’m still getting the same output – when the first loop runs on a category page, the output of print_r($do_not_duplicate) is: Array ( [0] => 353 [1] => 321 [2] => 194 [3] => 202 [4] => 112 [5] => 12 [6] => 10 ). But the output of print_r($do_not_duplicate) in the sidebar, just after <?php global $do_not_duplicate; ?> is still empty. Do I need to change anything about how the $do_not_duplicate variable is stored in the original array?

    In some themes, the sidebar is called before the main loop. I don’t know if that is the case with TT.

    Also, you may need to declare $do_not_duplicate as global before the main loop.

    The sidebar in Twenty Ten is called after the main loop.

    I’m still fairly new to WordPress and php. How and where would I declare $do_not_duplicate as global before the main loop?

    Does it involve a change to this code just before the main loop?

    <?php 	$do_not_duplicate = array();
    	while (have_posts()) : the_post();
    	$do_not_duplicate[] = get_the_ID();
     ?>

    Thanks.

    Try this:

    <?php
       global $do_not_duplicate;
       $do_not_duplicate = array();
       while (have_posts()) : the_post();
          $do_not_duplicate[] = get_the_ID();
     ?>

    That did it! Getting this piece of code working has been the last holdup to going live with the update to my child theme.
    There are several places on the web that explain how to run multiple loops on a page and prevent duplicate posts, but none of them worked without storing this as a global variable.
    Many Thanks!
    I’m going to wait to mark this thread as resolved temporarily just to be sure everything is good.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Second Loop in Sidebar of Twenty Ten Child Theme’ is closed to new replies.
Skip to toolbar