为什么get_posts()只返回5个匹配的帖子,它应该返回9?

Sco*_*t B 9 wordpress

global $post; 
$cat1=get_cat_ID('test1'); 
$cat2=get_cat_ID('test2'); 
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2",'showposts' => 5));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2"));
$myrecentpostscount = count($myrecentposts2);
echo $myrecentpostscount;
Run Code Online (Sandbox Code Playgroud)

echo的值为5(正确的值应为9).我可以让它返回正确的后计数值的唯一方法是更改​​$ myrecentposts2计算,如下所示...

$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2",'showposts' => 999));
Run Code Online (Sandbox Code Playgroud)

rom*_*man 17

Wordpress codex get_posts的默认posts_per_page值为5.

要删除此限制使用posts_per_page = -1.

要删除此限制,您可以使用nopaging = true.


小智 7

查看get_posts()Codex上的文档,您可以看到有关要显示的帖子数量的参数:

参数是:'posts_per_page'
用法:'posts_per_page'=> -1 // for removing the limit.这将获取所有帖子.

更新:'nopaging' => true这是更新版本的方式