WordPress - 限制 get_pages 显示 5 个项目

发布于 2024-11-10 07:10:54 字数 603 浏览 0 评论 0原文

如何限制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赴月观长安 2024-11-17 07:10:54

我知道这是一个老问题,但无论如何我都会发布它,以防有人试图限制结果并遇到同样的问题:

我不知道这是一个错误还是预期的功能,但是当您使用 get_pages 时“child_of”或当“hierarchical”为 TRUE 时,get_pages 也会尝试获取孙子,这会返回 NULL。

如果您不需要孙子,请将“child_of”替换为“parent”并将“hierarchical”添加为FALSE,因此您的查询将如下所示:

$pages = get_pages(array(
   'post_type' => 'page',
   'sort_column' => 'menu_order',
   'sort_order' => 'ASC',
   'parent' => 765,
   '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:

$pages = get_pages(array(
   'post_type' => 'page',
   'sort_column' => 'menu_order',
   'sort_order' => 'ASC',
   'parent' => 765,
   'hierarchical' => FALSE
));

This should work.

WordPress 3.4.2

狂之美人 2024-11-17 07:10:54

确保您运行的是 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

太傻旳人生 2024-11-17 07:10:54

get_pages() 中的 number 属性似乎有错误,

我通过使用 get_children

$pages = get_children(array(
   'numberposts' =>1,
   'post_parent' => $heading_page->post_id
   ));

并使用 post_parent 而不是 child_of

There seems to be an error with the number attribute in get_pages()

I got around it by using get_children:

$pages = get_children(array(
   'numberposts' =>1,
   'post_parent' => $heading_page->post_id
   ));

and using post_parent instead of child_of.

比忠 2024-11-17 07:10:54

根据手册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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文