Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active April 13, 2020 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/1d17daeaf8af64084a4c6af56cb5882d to your computer and use it in GitHub Desktop.
Save billerickson/1d17daeaf8af64084a4c6af56cb5882d to your computer and use it in GitHub Desktop.
<?php
/**
* Custom posts per page
*
*/
function be_posts_per_page( $query ) {
// Only run on archive pages
if( ! $query->is_main_query() || is_admin() || ! ( $query->is_home() || $query->is_archive ) )
return;
$posts_first_page = 3;
$posts_per_page = 9;
$page = $query->query_vars['paged'];
// First Page
if( ! $page ) {
$query->set( 'posts_per_page', $posts_first_page );
// Other Pages
} else {
$query->set( 'posts_per_page', $posts_per_page );
$query->set( 'offset', $posts_first_page + $posts_per_page * ( $page - 2 ) );
}
}
add_action( 'pre_get_posts', 'be_posts_per_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment