Multiple loops in different template files without duplicates?
-
Hi,
I know preventing duplicate posts appearing in multiple loops has been discussed plenty, but I’m yet to find an answer to this one:
I’m trying to create an additional loop in my footer.php file that doesn’t duplicate the content of the main loop. I suspect that the reason this isn’t working is because the loops are in separate template files – is that right?
I’ve laid out the code in each file below so you can see what I’m doing.
Thanks
Main loop on homepage
<div id="hp-latest"> <h1>Latest news</h1> <?php // Set posts to only show only 4 latest news query_posts('posts_per_page=4&cat=1'); // Start loop while ( have_posts() ) : the_post(); // Set the posts to not use in other loops $do_not_duplicate[] = $post->ID; ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div class="left"> <div class="entry-meta"> <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(100,100), array("class" => "alignleft post_thumbnail")); } else { echo '<img src="' . get_bloginfo('template_directory') . '/images/hp-thumb-main.gif" height="100" width="100" />'; } ?> <span class="the-date"> <?php echo get_the_date('d/m/y'); ?> </span> <span class="cat-links"> <?php echo get_the_category_list( ', ' ); ?> </span> <span class="the-author"> <?php the_author_posts_link(); ?> </span> </div><!-- .entry-meta --> </div> <div class="right"> <h2 class="entry-title"> <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'templatename' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a> </h2> <div class="entry-content"> <?php // homemade function to format excerpt mynew_excerpt(100); ?> </div><!-- .entry-content --> </div> </div> <!-- End div#post-ID --> <?php endwhile; // End the loop. // Reset query wp_reset_query(); ?>Footer.php file
<ul class="fbox"> <li class="heading"><h3>Other news</h3></li> <li class="description">Other news from all regions</li> <?php // This is the additional loop $my_query = new WP_Query('cat=1&showposts=5'); while ($my_query->have_posts()) : $my_query->the_post(); if ( !in_array( $post->ID, $do_not_duplicate ) ) { ?> <li> <h2 class="entry-title"> <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'templatename' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a> </h2> </li> <?php } endwhile; ?> <li class="highlight"><a href="<?php bloginfo('url') ?>/?cat=1">See all news</a></li> </ul>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Multiple loops in different template files without duplicates?’ is closed to new replies.