Wordpress - Pagination when using wp_query?
Replace <!-- WHAT GOES HERE?????? -->
with the pagination code below:
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
WordPress comes with a handy function called paginate_links()
which does the heavy lifting. In the example above, the custom WP_Query object $query
is used instead of the global $wp_query
object.
This code is for Custom Query Pagination. You can follow the steps to create your own pagination in WordPress.
<?php
/**
* Template Name: Custom Page
*/
get_header(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 4,
'paged' => $paged
);
$custom_query = new WP_Query( $args );
?>
<!----start-------->
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<div>
<ul>
<li>
<h3><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h3>
<div>
<ul>
<div><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a></div>
</ul>
<ul>
<p><?php echo the_content(); ?></p>
</ul>
</div>
<div>
</li>
</ul>
</div> <!-- end blog posts -->
<?php endwhile; ?>
<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
} ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
<!----end-------->
<?php get_footer();
Reference : https://www.wpblog.com/use-wp_query-to-create-pagination/