Best way to prevent post duplication when using multiple loops
-
25-01-2021 - |
Question
I have the following loops on a page on my website.
Whats the best way to adapt the code below to stop the post in my first loop, being repeated in the second?
<?php query_posts('showposts=1&post_type=post'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="latestpost" <?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}
?>>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h2><?php the_title() ;?></h2>
<div class="summary" style="color:#fff;">
<?php the_excerpt(); ?>
</div>
<!-- <?php the_post_thumbnail(); ?> -->
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
</a><!-- permalink -->
<?php endif; ?>
</article>
</div><!-- latest post -->
<?php wp_reset_query(); ?>
<div id="postlist">
<h2>Personal finances</h2>
<?php query_posts('showposts=3&post_type=post&category_name=Personal');
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- post title -->
<div class="post-thumbnail">
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
</a>
<?php endif; ?>
<!-- /post thumbnail -->
</div>
<h3 class="postlist-article-header">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h3>
<!-- /post title -->
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
<?php wp_reset_query(); ?>
La solution
You can gather the post id(s) from the first loop in an array and then use post__not_in arg in the second loop.
$exclude = array();
if ( have_posts() ) : while ( have_posts() ) : the_post();
//first loop
$exclude[] = get_the_ID();
...
//second loop
$second_loop_args = array(
'showposts' => 3,
'post_type' => 'post',
'category_name' => 'Personal', //should make sure you're using category slug here
'post__not_in' => $exclude //this arg needs to be an array, hence moving from string to array format for query_post args
);
query_posts($second_loop_args);
....
**I will say though, that you should reconsider using new WP_Query() for your loops instead of query_posts as detailed here: https://codex.wordpress.org/Class_Reference/WP_Query
It's basicaly the same, but instead of using query_posts, you just assign a variable your new WP_Query class and loop through it.
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange