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();
}
}