Using pagination with get_posts on page type

You should check your code because you have some sentences that will make your pagination function returns empty value. For example, the next piece of code will get you out because you are in a page template, so is_singular() returns true:

if( is_singular() )
    return;

Also, you are using the global $wp_query object inside your pagination function but you are using get_posts() to get the list of pages, not the main query. So $wp_query->max_num_pages is cero because the main query was for a single page. So this piece of code will also get you out:

global $wp_query;

/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
    return;

Removing both blocks of code won’t be enough because you use again the global $wp_query object to get the max number of pages:

 $max = intval( $wp_query->max_num_pages );

Also, if you set the page template as the front page of your blog, the paged var won’t work in the query, you should use the page query var.

I recommend you to think again about what you are trying to do. Looking at your code, you are trying to list the child pages of the page with ID 7 and paginate the results. It seems easier convert that child pages to normal posts under a category and display them in a standard category template.

If you really need the items be child pages, in your case it should be better to use a new WP_Query() object, not get_posts() function and get the max number of pages to your pagination function:

 $args =  array(
       'cild_of' => '7',
       'meta_key' => 'Type',
       'post_type' => 'page',
       'post_status' => 'publish',
       'orderby' => 'meta_value',
       'order' => 'ASC',
       'posts_per_page' => '20',
       'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
   );
   $query = new WP_Query($args);
   $max_num_pages = $query->max_num_pages;

   wpbeginner_numeric_posts_nav($max_num_pages);

And the pagination function:

function wpbeginner_numeric_posts_nav($max_num_pages = 0) {

/** Stop execution if there's only 1 page */
if( $max_num_pages <= 1 )
    return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;

/** Add current page to the array */
if ( $paged >= 1 )
    $links[] = $paged;

/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max_num_pages ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
    printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? ' class="active"' : '';
    printf( '<li%s><a href="https://wordpress.stackexchange.com/questions/137100/%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
    if ( ! in_array( 2, $links ) )
        echo '<li>…</li>';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
    $class = $paged == $link ? ' class="active"' : '';
    printf( '<li%s><a href="https://wordpress.stackexchange.com/questions/137100/%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max_num_pages, $links ) ) {
    if ( ! in_array( $max_num_pages - 1, $links ) )
        echo '<li>…</li>' . "\n";
    $class = $paged == $max_num_pages ? ' class="active"' : '';
    printf( '<li%s><a href="https://wordpress.stackexchange.com/questions/137100/%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max_num_pages ) ), $max_num_pages );
}

/** Next Post Link */
if ( get_next_posts_link() )
    printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}

Leave a Comment