The heading says a lot, so I'll repeat it here "How do I paginate a get_posts() request inside another page loop?"

I'm using a page to display child-pages and I need to paginate them.

Edit: Should I use a custom WP_Query instead? Would this make any difference?

The code I use inside the normal page loop looks like this right now:

// Set up the arguments for retrieving the pages
$args = array(
    'post_type' => 'page',
    'numberposts' => -1,
    'posts_per_page' => 1, 
    'paged' . get_query_var('page'),
    // $post->ID gets the ID of the current page
    'post_parent' => $post->ID,
    'order' => ASC,
    'orderby' => title
$subpages = get_posts($args);
// Just another WordPress Loop
$child_counter = 1;

foreach($subpages as $post) :

    if ($child_counter == 2 || (($child_counter)%3)-2 == 0) {
        echo '<div class="entry-child center-menu-item">';
    } else {
        echo '<div class="entry-child">';

    <h2><?php the_title(); ?></h2>
    <p><?php the_excerpt_max_charlength(200); ?></p>
    <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php _e('Les mer', 'monster'); ?><span id="arrow-right"></span></a></p>
    <div class="navigation">
        <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
        <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?>        
<?php endforeach; ?>

I can't get my head to understand how I can retrieve the pagination information for the child loop and not the main loop. So I have no clue how to paginate the childpages. Remember this get_posts() call is inside a normal page loop.

Happy for all answers, hints or tips!

  • Have you got another request variable setup? I'm assuming you're feeding get_posts the main page variable which is setup for the main loop. Have you got another variable?
    – soulseekah
    Commented Mar 24, 2012 at 13:36
  • Above the this code is the standard page loop that wraps around this loop. <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> - this request query is the unmodified WordPress page request. was that the answer you where looking for? Did not really understand your question? can you clarify? Commented Mar 24, 2012 at 14:19
  • Well, you're feeding 'paged' . get_query_var('page') [which should be 'paged' => get_query_var('page') with the arrow], that is the page of the main loop; how do you keep track of the sub-page?
    – soulseekah
    Commented Mar 24, 2012 at 14:28
  • Good question, I don't know how i can get the pagination information for sub-pages... Got any suggestions? Commented Mar 24, 2012 at 21:35
  • I've wrote an answer I about paginating attachments. And another answer on how to make a shortcode out of this, which gives the ability to show this inside the main loop. Although this those are about attachments, the method can be adapted for childpages. At least it should get you started on how to do this. Commented Nov 29, 2013 at 13:35


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.