My home page has a section at the top that displays a featured post from a time period if it has been set, otherwise it displays the most commented post of a time period, or it just displays the most recent post if none of the above are true.
I then have two more loops on the page which display the 6 most recents posts (3 each).
I am using the following code to avoid duplicate posts being displayed.
<?php
$do_not_duplicate = $post->ID;
if( $post->ID == $do_not_duplicate ) continue;
?>
This does the job of avoiding duplicate posts by skipping over the post that matches the ID, however it leaves a blank space that makes the homepage look odd. Is there a way to display the next most recent post that isn't that ID, rather than skipping over it entirely?
<div class="grid_8 main-content">
<div class="grid_12 alpha omega">
<?php
// Fetch from the 'featured' category
$args = array(
'posts_per_page' => 1,
'category_name' => 'featured',
'date_query' => array( 'after' => '1 day ago' )
);
$the_query = new WP_Query( $args );
// If no featured post is found fetch the most commented
if ( 0 === $the_query->found_posts ) {
$args = array(
'posts_per_page' => 1,
'orderby' => 'comment_count',
'date_query' => array( 'after' => '1 day ago' )
);
$the_query = new WP_Query( $args );
}
// If no most commented posts are available fetch the most recent
if ( 0 === $the_query->found_posts ) {
$args = array(
'posts_per_page' => 1,
'orderby' => 'most_recent'
);
$the_query = new WP_Query( $args );
}
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $do_not_duplicate = $post->ID; ?>
<a href="<?php the_permalink();?>"><h1 class="main-heading"><?php the_title(); ?></h1></a>
<p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
<?php the_excerpt(); ?>
<hr>
<?php
endwhile;
wp_reset_postdata();
else:
_e('No posts found');
endif; ?>
</div>
<?php
$args = array(
'posts_per_page' => 3,
'orderby' => 'most_recent'
);
$the_query = new WP_Query( $args );
?>
<div class="recent_posts grid_6 alpha">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
<p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
<?php the_excerpt(); ?>
<hr>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php
$exclude = $most_recent;
$args = array(
'posts_per_page' => 3,
'orderby' => 'most_recent',
'offset' => 3
);
$the_query = new WP_Query( $args );
?>
<div class="grid_6 alpha">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( $post->ID == $do_not_duplicate ) continue; ?>
<a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
<p class="meta">Published on <?php the_date(); ?> by <a href="<?php echo the_permalink();?>"><?php the_author(); ?></p></a><a href="<?php the_permalink();?>"> <?php comments_number(); ?></a></p>
<?php the_excerpt(); ?>
<hr>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div>