• 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)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can make “$do_not_duplicate” a global variable by:
    <?php global $do_not_duplicate; ?>. Put this at the top of both template files. Or in stead of <?php get_footer(); ?> use <?php include('footer.php');?> and you can use the variable on both template files

    Thread Starter Osu

    (@mrnabo)

    That worked a treat! Thanks!

    Only thing is that it was counting the number of posts in the main loop as well as the additional one, meaning that after I added global $do_not_duplicate; to both the main loop and footer template files, I had to change the footer.php file to this:

    <ul class="fbox">
    				<li class="heading"><h3>Other news</h3></li>
    				<li class="description">Other news from the all regions</li>
    				<?php
    				// This is the additional loop
    
    				// To maintain five posts in the footer 'Other news' box, need to add count to WP_Query argument
    				// as
    				$fiveposts = count($do_not_duplicate);
    				$fiveposts = $fiveposts + 5;
    				$my_query = new WP_Query('cat=1&showposts=' . $fiveposts . '');
    
    				 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>

    By the way, out of interest, is there any security issues with making variables global like this?

    Moderator keesiemeijer

    (@keesiemeijer)

    There is no security issue, but in general it is bad practice to use globals. Plugins could use the same variable and break your site. So I would use the other solution of using: <?php include('footer.php');?>

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Multiple loops in different template files without duplicates?’ is closed to new replies.