WordPress.org

Ready to get started?Download WordPress

Forums

Prevent the Same Post from Showing in Multiple "Highlights" Sections (3 posts)

  1. cridge
    Member
    Posted 3 years ago #

    I'm trying to help a friend with this problem....

    He is using a theme which contains 3 highlights sections on the homepage. The user assigns each Highlights section a specific category in the theme options panel, so each of the 3 sections display the last post from their assigned category.

    The problem is that his site only has 4 categories and many of the posts are assigned to all the same categories. Therefore, the same post often appears in 2 or sometimes all 3 of the highlights sections.

    Is there code that can be added that will prevent the same post from showing more than once? Something that will say: if post A is displayed in highlights #1, do not show post A in highlights #2 or #3.

    The website in question is: http://www.sgpspclub.com/ and the code for the highlights section is:

    <!-- Highlights // -->
    		<span class="heading2"><span>Highlights</span></span>
    		<ul class="highlights">
    		<?php $highlight1 = new WP_Query("meta_key=thumbnail&cat=".get_wpn_config('highlights_category_id_1')."&showposts=1"); while($highlight1->have_posts()) : $highlight1->the_post(); ?>
    			<li>
    			<?php if(get_post_meta($post->ID, "thumbnail", true)) : ?>
    				<a href="<?php the_permalink(); ?>" class="thumbnail"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /></a>
    			<?php endif; ?>
    				<span class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    				<span class="cat"><?php the_category(', '); ?></span>
    			</li>
    		<?php endwhile; ?>
    
    		<?php $highlight2 = new WP_Query("meta_key=thumbnail&cat=".get_wpn_config('highlights_category_id_2')."&showposts=1"); while($highlight2->have_posts()) : $highlight2->the_post();?>
    			<li>
    			<?php if(get_post_meta($post->ID, "thumbnail", true)) : ?>
    				<a href="<?php the_permalink(); ?>" class="thumbnail"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /></a>
    			<?php endif; ?>
    				<span class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    				<span class="cat"><?php the_category(', '); ?></span>
    			</li>
    		<?php endwhile; ?>
    
    		<?php $highlight3 = new WP_Query("meta_key=thumbnail&cat=".get_wpn_config('highlights_category_id_3')."&showposts=1"); while($highlight3->have_posts()) : $highlight3->the_post();?>
    			<li>
    			<?php if(get_post_meta($post->ID, "thumbnail", true)) : ?>
    				<a href="<?php the_permalink(); ?>" class="thumbnail"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /></a>
    			<?php endif; ?>
    				<span class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    				<span class="cat"><?php the_category(', '); ?></span>
    			</li>
    		<?php endwhile; ?>
    
    		</ul>
    		<div class="clear"></div>
    		<!-- // Highlights -->
    		<?php endif; ?>

    Help is greatly appreciated!!

  2. Michelle Langston
    Theme Wrangler at Automattic
    Posted 3 years ago #

    Yes, there is a way to prevent this from happening. You will need to rewrite the loops slightly. The loop must collect the IDs of posts that have already showed up and store them in an array. Then, each subsequent loop must check the array to see if a post ID has been stored already. Here is the Codex's page on this: http://codex.wordpress.org/The_Loop#Multiple_Loops (there's a section there that explains how to avoid duplicate posts).

  3. cridge
    Member
    Posted 3 years ago #

    thank you very much for your response. i'll look into that.

Topic Closed

This topic has been closed to new replies.

About this Topic