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();
?>
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
.
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?
Make sure you are running wordpress 2.8 Since number
was added after that version:
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