If you look my sitemap, you’ll see the list of my post with pagination. That page was created under custom page template, the custom WP_Query of post’s list build under main loop of page, and function the_posts_pagination to display pagination.
Here the sample PHP code that you can accomplish if you need create custom page template with pagination. Just add after your main query loop.
$page_id = get_the_ID();
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'book', 'movie' ), // your post type
'post_status' => 'publish',
'posts_per_page' => 5,
'paged' => $paged,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo sprintf( '<h2>%1$s %2$s</h2><ul>',
intval( $query->found_posts ),
__( 'Articles' )
);
while ( $query->have_posts() ) : $query->the_post();
?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php the_title( sprintf( '<a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a>' ); ?></li>
<!-- run your stuff here -->
<?php
endwhile;
echo '</ul>';
$GLOBALS['wp_query'] = $query;
$args = array(
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>',
'base' => user_trailingslashit( trailingslashit( get_page_link( $page_id ) ) . 'page/%#%' )
);
the_posts_pagination( $args );
else :
// your stuff if no post found
endif;
wp_reset_query(); // reset our query
We using get_page_link on paging arguments code instead of get_pagenum_link function, it just to making work with main loop if using post page ( <--nextpage--> ) for your content. I think there are better clean approach than this, but.. ah, let’s go ready to rumble.
Share