开发者

Wordpress, The loop: display just 2 posts

开发者 https://www.devze.com 2023-02-18 22:19 出处:网络
What\'s the best way to show just 2 posts in the loop? <?php $i = 1; while (have_po开发者_StackOverflow中文版sts()):

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

<?php
$i = 1;
while (have_po开发者_StackOverflow中文版sts()):
    if ($i == 3)
    {
        break;
    }
    the_post();
    $i++;
endwhile;
?>

Is there something more "beatiful" ?


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.


Try this:

<?php
// Printing post number 1 and 2
for($i = 1; $i < 3; $i++){
    the_post();
}
?>


for($i = 0; $i < 2; ++$i) {
       post_here();
}


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号