WordPress add pagination for custom loop that show subpages

I have one page (not an article) with n subpages.
In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.

How can I do that?

Read More

This is my simple code now:

<?php 
    $parent_id = 14; //main page id
    $pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
    foreach ( $pages as $page ) : ?>
        <div class="item">
            <div class="item-title">
                <h2><a href="<?php echo get_permalink( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
            </div>
        </div>
    <?php endforeach; ?>

Thanks.

Related posts

Leave a Reply

3 comments

  1. I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().

    Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:

        <?php
    
        // Pagination variable
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        // The Query
        $the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
    
        // The Loop
        if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
        global $post;
        $thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
            ?>
        <div id="side-post-title">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </div>
    
         <div id="side-post-excerpt">
                   <?php the_excerpt(); ?>
    
                 <a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more"> 
                           <img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>                                                          
          </div> 
    
    
    
    <?php endwhile; endif; ?>
    
    <nav class="navigation">
       <div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
       <div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
    </nav>