How do I paginate a get_posts() request inside another page loop (Paginating childpages)

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.

Read More

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) :
    setup_postdata($post);

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

    <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>
    <div class="navigation">
        <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
        <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?>        
    </div>
    </div>
<?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!

Related posts

Leave a Reply