Reputation: 33
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" ?
Upvotes: 3
Views: 3730
Reputation: 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);
Upvotes: 0
Reputation: 4709
Try this:
<?php
// Printing post number 1 and 2
for($i = 1; $i < 3; $i++){
the_post();
}
?>
Upvotes: 0
Reputation: 25060
Use query_posts():
query_posts('posts_per_page=2');
before the loop.
Excerpt from the documentation page:
query_posts() can be used to display different posts than those that would normally show up at a specific URL.
Upvotes: 7