Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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) :
    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!

share|improve this question
    
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 Mar 24 '12 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? –  Ole Henrik Skogstrøm Mar 24 '12 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 Mar 24 '12 at 14:28
    
Good question, I don't know how i can get the pagination information for sub-pages... Got any suggestions? –  Ole Henrik Skogstrøm Mar 24 '12 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. –  ialocin Nov 29 '13 at 13:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.