Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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