Show always 5 posts even if I do not show the current post if it is selected randomly

With the code below, I show 5 posts ordered randomly at the end of a post. As you can see, if the current post (ID) is one of the random chosen posts then it doesn’t show up.

That means instead of the 5 posts I want to show there are 4. In other case, there will be 5 posts.

Read More

My question is how to edit the code below to show only 5 posts even if the current post is one of the randomly chosen.

<?php
query_posts( 'posts_per_page=5&orderby=rand' );

while (have_posts()) : the_post();
    if ( $post->ID == $ID  ) continue;
the_title();
endwhile;
    wp_reset_query(); 
?>

Related posts

Leave a Reply

1 comment

  1. Select 1 extra post anticipated and only show it if you had to skip one.

    -edit-
    This is very ugly but it should work:

    <?php
        query_posts( 'posts_per_page=6&orderby=rand' );
        $counter = 0;
        while (have_posts()) : the_post();
            if ( $post->ID == $ID  || $counter == 5 ) continue;
            $counter++;
            the_title();
        endwhile;
        wp_reset_query(); 
    ?>