Wordpress,循环:只显示2个帖子

tho*_*hom 3 php wordpress

什么是在循环中只显示2个帖子的最佳方式?

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

有更"美丽"的东西吗?

Mat*_*iva 7

使用query_posts():

query_posts('posts_per_page=2');
Run Code Online (Sandbox Code Playgroud)

在循环之前.

摘自文档页面:

query_posts()可用于显示与通常在特定URL上显示的帖子不同的帖子.