WP Query – Alternative Number Of Posts On Paged Results

Oct 17, 2018

WP Query – Alternative Number Of Posts On Paged Results


This post was originally created on our sister site WP Cover.
View Original Article
October 17, 2018

Have you ever needed to show an alternative number of posts on the first page of the results versus the paged results?  It’s kind of a design trend right now by giving the most recent post prominance but in return, you often give an uneven number of posts within your grid.  Luckily for you, the fix is simple and clean:

My initial query looked something like this:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
  'post_type' => 'my-post-type',
  'posts_per_page' => 10,
  'paged' => $paged
);
$the_query = new WP_Query( $query_args );

To modify the limit based on whether or not the results had been paged, my modification was simply:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
if( $paged == 1 ) {
   $limit = 10;
} else {
   $limit = 9;
}
$query_args = array(
  'post_type' => 'my-post-type',
  'posts_per_page' => $limit,
  'paged' => $paged
);
$the_query = new WP_Query( $query_args );

BOOM! Go enjoy the rest of your day :).

The post WP Query – Alternative Number Of Posts On Paged Results appeared first on WP Cover.

Additional News

Perfect!

Let's get started. Fill out the form below to email us or request a call back.