Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active Apr 13, 2020
Embed
What would you like to do?
<?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