0

I followed this method of getting images from the media library.

// get the first image attached to the current post
function aldenta_get_post_image($size = 'thumbnail') {
    global $post;

    $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    if ($photos) {
        $photo = array_shift($photos);
        return wp_get_attachment_image($photo->ID, $size);
    }

    return false;
}

But I want to paginate the results, how can I do that?

  • Paginate it based on what? A set query var? A value passed into the function? – t31os Mar 9 '11 at 15:59
  • @t31os, currently, if there are 100 images, it will show them all, I want to limit the results to something like 20 maybe? so that the user wont see 100s of images at once – JM at Work Mar 10 '11 at 2:18
  • Did you post the wrong function then, the above function looks like it would only give a single result. Also, you unfortunately didn't answer my question(see previous comment). – t31os Mar 10 '11 at 11:37
1

Use the 'offset' parameter like in get_posts(). You can take the number of the page you're on as value.

  • This assumes your page is $paged. How do you break up a get_children query (or get_posts) to be paged? – AlxVallejo Sep 28 '12 at 15:49
  • The same way. You have the current page number stored somewhere. Otherwise the code is broken already. – fuxia Sep 28 '12 at 15:51
  • 'posts_per_page' does not work in get_pages, like it does in wp_query. You can limit the query, but not the display. – AlxVallejo Sep 28 '12 at 17:04
0

get_children() returns an array of posts, not a query that can be looped through and therefore paginated.

I would encourage you to use WP_Query instead. This WPSE answer should provide a code snippet to get you started. Then it should just be a matter of looping through the query and using standard WP paging.

  • Downside to WP_Query is that there is no hierarchical argument. You can only get children 1 layer deep. – AlxVallejo Sep 28 '12 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.