[英]Wordpress, The loop: display just 2 posts
什么是在循环中只显示2个帖子的最佳方式?
<?php
$i = 1;
while (have_posts()):
if ($i == 3)
{
break;
}
the_post();
$i++;
endwhile;
?>
有更“美丽”的东西吗?
使用query_posts() :
query_posts('posts_per_page=2');
在循环之前。
摘自文档页面:
query_posts()可用于显示与通常在特定URL上显示的帖子不同的帖子。
试试这个:
<?php
// Printing post number 1 and 2
for($i = 1; $i < 3; $i++){
the_post();
}
?>
for($i = 0; $i < 2; ++$i) {
post_here();
}
您可以使用WP_Query类,以避免将globals帖子与自定义循环混合
$results = new WP_Query('posts_per_page');
if ($results->have-posts()) {
while ($results->have_posts()) {
$results->the_post();
the_title();
}
}
unset($results);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.