WordPress - 限制 get_pages 显示 5 个项目
如何限制 get_pages 仅显示 5 个项目?
我想添加 'number' =>; 5
到数组会将其限制为 5,但它不会显示任何内容。这是我的代码:
<?php
$pages = get_pages(array('post_type' => 'page','sort_column' => 'menu_order','sort_order' => 'ASC','child_of' => 765));
foreach($pages as $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<p><?php echo $fields->start_date; ?> <a href="<?php echo get_page_link($post->ID) ?>"><?php echo substr($fields->event_title,0,24) . "..."; ?></a></p>
<?php
}
wp_reset_query();
?>
How can I limit the get_pages to only show 5 items?
I thought adding 'number' => 5
to the array would limit it to 5 but it doesn't show anything. Here's my code:
<?php
$pages = get_pages(array('post_type' => 'page','sort_column' => 'menu_order','sort_order' => 'ASC','child_of' => 765));
foreach($pages as $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<p><?php echo $fields->start_date; ?> <a href="<?php echo get_page_link($post->ID) ?>"><?php echo substr($fields->event_title,0,24) . "..."; ?></a></p>
<?php
}
wp_reset_query();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个老问题,但无论如何我都会发布它,以防有人试图限制结果并遇到同样的问题:
我不知道这是一个错误还是预期的功能,但是当您使用 get_pages 时“child_of”或当“hierarchical”为 TRUE 时,get_pages 也会尝试获取孙子,这会返回 NULL。
如果您不需要孙子,请将“child_of”替换为“parent”并将“hierarchical”添加为FALSE,因此您的查询将如下所示:
这应该可行。
WordPress 3.4.2
I know this is an old question, but I'm posting it anyway in case somebody will try to limit results and will run into the same issue:
I don't know if it's an error or intended functionality, but when you use get_pages with a "child_of" or when "hierarchical" is TRUE, get_pages also tries to get the grandchildren as well, which returns NULL.
If you don't need the grandchildren, replace "child_of" with "parent" and add "hierarchical" as FALSE, so your query will be like that:
This should work.
WordPress 3.4.2
确保您运行的是 WordPress 2.8,因为
number
是在该版本之后添加的:http:// /codex.wordpress.org/Function_Reference/get_pages
Make sure you are running wordpress 2.8 Since
number
was added after that version:http://codex.wordpress.org/Function_Reference/get_pages
get_pages()
中的number
属性似乎有错误,我通过使用 get_children:
并使用
post_parent
而不是child_of
。There seems to be an error with the
number
attribute inget_pages()
I got around it by using get_children:
and using
post_parent
instead ofchild_of
.根据手册,
number
确实是正确的参数。它是在 2.8 中添加的。也许您正在运行旧版本的 Wordpress?
According to the manual,
number
is indeed the correct parameter.It was added in 2.8. Maybe you are running an older version of Wordpress?