You can use the post__not_in
parameter of WP_Query
to exclude an array of post IDs from the query.
get_the_category()
returns an array of term objects, so the get_term_by
is redundant. Also is_single()
will always be true in content-single.php
(not strictly speaking true but only not so if you are interferring with the template selection).
(Finally, in your example, if you have at most 4 posts, then (2==$count || 4==$count)
is equivalent to ($count%2 == 0)
)
<?php
$count = 0;
$cat = get_the_category();
if( $cat ){
$featured_posts = new WP_Query(array(
'post__not_in'=>array(get_the_ID()),
'category_name' => $cat->slug,
'posts_per_page' => 4
));
while ( $featured_posts->have_posts() ): $featured_posts->the_post();
$count++;
$no_margin = ($count%2 == 0) ? ' no-margin-right' : '';
?>
<div class="latest-category-posts-image-t<?php echo $no_margin; ?>">
<a href="https://wordpress.stackexchange.com/questions/57488/<?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();
}
?>