0

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[0]->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.

1 Answer 1

2

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="<?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();
    }
?>
3
  • 1
    Need I say it? Great answer, as always. Thanks! :)
    – its_me
    Jul 5, 2012 at 10:43
  • Isn't the closing brace } for the conditional tag missing?
    – its_me
    Jul 5, 2012 at 12:51
  • 1
    ye... ahem, I mean no... Jul 5, 2012 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.