WordPress, The loop: display just 2 posts

What’s the best way to show just 2 posts in the loop?

<?php
$i = 1;
while (have_posts()):
    if ($i == 3)
    {
        break;
    }
    the_post();
    $i++;
endwhile;
?>

Is there something more “beatiful” ?

Related posts

Leave a Reply

4 comments

  1. You can use the WP_Query class, to avoid mixing the globals posts with your custom loop

    $results = new WP_Query('posts_per_page');
    
    if ($results->have-posts()) {
        while ($results->have_posts()) {
            $results->the_post();
            the_title();
        }
    }
    
    unset($results);