0

I am using the following code to list the child pages of the current page being viewed. But it only outputs the last 5 child pages; i want it to list all pages in blocks of 5 with "next" and "previous" links. Such as it works on normal posts. The code I am using is the following:

<?php 
$ancestors = array();
$ancestors = get_ancestors($post->ID,'page');
$parent = (!empty($ancestors)) ? array_pop($ancestors) : $post->ID;
if (!empty($parent)) {
  $kids = new WP_Query(
    array(
      'post_parent'=>$parent,
      'post_type' => 'page',
      'ignore_sticky_posts' => true
    )
  );
  if ($kids->have_posts()) {
    while ($kids->have_posts()) {
      $kids->the_post();
      echo '<li>' . '<div>' . '<a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a>' . '<span>' . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '</span><br/>';
      echo '<div class="extrainfo">';
      echo the_field("info_add");  
      echo '</div>';
      echo '</div></li>';
    }
  }
}
?>

The page in question is this one: http://dedos.info/flavioc/trabalhos/

Now I'm wondering if I should have used Custom Post Types to manage this but now it's too late. Any help will be appreciated, thanks.

2

Option 1:

Personally I prefer this, because it gives you more options in the long run. Especially considering other setups involving a secondary query, take a look at my other answer I linked below.

What you need to do is, make use of the paged parameter WP_Query offers. Additionally you have to setup a variable - here $paged - representing the pagination. Having this, you can use paginate_links() to construct the pagination links.

Code:

global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ancestors = array();
$ancestors = get_ancestors($post->ID,'page');
$parent = (!empty($ancestors)) ? array_pop($ancestors) : $post->ID;
if (!empty($parent)) {
    $kids = new WP_Query(
        array(
            'post_parent'=>$parent,
            'post_type' => 'page',
            'ignore_sticky_posts' => true,
            'paged' => $paged
        )
    );
    if ($kids->have_posts()) {
        while ($kids->have_posts()) {
            $kids->the_post();
            echo '<li>' . '<div>' . '<a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a>' . '<span>' . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '</span><br/>';
            echo '<div class="extrainfo">';
            echo the_field("info_add");  
            echo '</div>';
            echo '</div></li>';
        }
        if ( get_option('permalink_structure') ) {
            $format = 'page/%#%';
        } else {
            $format = '&paged=%#%';
        }
        $args = array(
            'base' => get_permalink( $post->post_parent ) . '%_%',
            'format' => $format,
            'current' => $paged,
            'total' => $kids->max_num_pages
        );
        echo paginate_links( $args );
        wp_reset_postdata();
    }
}

Note: untested

For additional options take a look at the according documentations. If you want to paginate a secondary query that is shown in addition to the main query separately to the latter, you have to do a couple more steps, I explained those lately on another question. If you - for example - would like a different rewrite, instead of page, which means you need to use a different query variable then paged, you also could find all the information you need to do this on the question I linked.

Option 2:

You can get pagination this way too. It still depends on setting up the parameter paged and the according variable, see above. If you want to use other pagination link functions wordpress offers, for example previous_posts_link/next_posts_link() or posts_nav_link, this is easier to implement. Key here is temporarily replacing $wp_query like shown below, because those functions by default - for more configuration options read the documentations - are accessing this object to generate the pagination links.

Code:

global $post, $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ancestors = array();
$ancestors = get_ancestors($post->ID,'page');
$parent = (!empty($ancestors)) ? array_pop($ancestors) : $post->ID;
if (!empty($parent)) {
    $kids = new WP_Query(
        array(
            'post_parent'=>$parent,
            'post_type' => 'page',
            'ignore_sticky_posts' => true,
            'paged' => $paged
        )
    );
    // Put default query object in a temp variable
    $tmp_query = $wp_query;
    // Now wipe it out completely
    $wp_query = null;
    // Re-populate the global with our custom query
    $wp_query = $kids;
    if ($kids->have_posts()) {
        while ($kids->have_posts()) {
            $kids->the_post();
            echo '<li>' . '<div>' . '<a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a>' . '<span>' . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '</span><br/>';
            echo '<div class="extrainfo">';
            echo the_field("info_add");  
            echo '</div>';
            echo '</div></li>';
        }
        posts_nav_link();
        // Restore original query object
        $wp_query = null;
        $wp_query = $tmp_query;
        wp_reset_postdata();
    }
}
8
  • thanks A LOT for the reply. it worked. there's a little catch though; if you could please visit this page dedos.info/flavioc/trabalhos and see the pagination links below, you'll see that the links have "absorbed" the last page listed in the url structure. the slug of the last page is getting in front of the /page/2 . i believe it should be simple to fix but i couldn't solve it.
    – dedos
    Dec 5 '13 at 11:27
  • i see it's in this line 'base' => get_permalink() . '%_%', the get_permalink is causing this issue but i don't know what to replace with.
    – dedos
    Dec 5 '13 at 11:32
  • I changed the code, try this. @dedos
    – Nicolai
    Dec 5 '13 at 11:34
  • PERFECT. thanks a lot. i wish i knew more so i could help out people like this. you saved this morning.
    – dedos
    Dec 5 '13 at 11:38
  • @something i tried both ways. I am facing strange issue. when i pass order_by to "menu_order" then some posts repeats. Why? May 2 '17 at 13:09

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.