Difficulty Level: IntermediateWP_Query and pagination is kinda tricky. The catch is in fetching the page number from the URL and feeding it to your loop so it knows what posts to display.
In this tutorial, you’ll see a method for doing just that to add as many WP_Query custom loops as you want… and make them functional on the same page.
Download VideoGrab the Snippet
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$args1 = array(
'paged' => $paged1,
'posts_per_page' => 2,
'category_name' => 'dogs'
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
In the next tutorial, I’ll show you how to add jQuery AJAX to make this incredibly smooth.
Additional Resources
Good Guy Boone’s personal website
How to Use WP_Query to Create Custom Loops in WordPress
WP Codex – paginate_links
