This is the code I use to display the 4 most recent posts under the category the current post belongs to (right below the current post):
<?php if (is_single()) : ?> <?php $count = 0; $aahan_category = get_the_category(); $aahan_category = get_term_by( 'name', $aahan_category->cat_name, 'category'); $some_featured_posts = new WP_Query(array('category_name' => $aahan_category->slug, 'posts_per_page' => 4)); while ($some_featured_posts->have_posts()): $some_featured_posts->the_post(); $count++; $no_margin = (2 == $count || 4 == $count) ? ' no-margin-right' : ''; ?> <div class="latest-category-posts-image-t<?php echo $no_margin; ?>"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_post_thumbnail( 'thumbnail' ); ?> <h1 class="latest-category-posts-text"><?php the_title(); ?></h1> </a> </div> <?php endwhile; wp_reset_postdata(); ?> <?php endif; ?>
In case it matters, I added this code in
content-single.php. The code basically shows the featured images (post-thumbnail) of the 4 most recent posts under the category the current post belongs to, along with their titles.
Now the question is, if the current post is among the
n most recent posts (
n = 4 as per my code), how do I exclude it from the listing?
PS: Although it goes without saying, it would be great if you can show how the code I use could be optimized.