The home page of my site is set to show the 9 latest posts (via the setting in the admin area), and then at the bottom I have the standard pagination links which allow visitors to view more posts.
I would like to have the home page (and only the home page) show 7 latest posts, but then on the subsequent pages - 2, 3, 4 etc have 9 posts showing. I also want the categories and archives to display 9 posts too.
All the methods I have tried so far either result in missing and duplicated posts, or I end up with an extra page showing in the pagination which results in a 404.
So far I've tried variations of the answer posted here:
Pagination returns 404 after page 20
And also here (Case #2: Conditional Offset):
Different 'posts_per_page' setting for first, and rest of the paginated pages?
Neither seems to work for what I want to achieve, but I feel like I'm close to a solution and could be missing something obvious.
Here's the most recent version of the code I tried (added to functions.php), which resulted in too many pagination links showing (the last link caused a 404):
function home_paged_offset( $query ) {
$ppp = get_option( 'posts_per_page' );
$first_page_ppp = 7;
$paged = $query->query_vars[ 'paged' ];
if( $query->is_home() && $query->is_main_query() ) {
if( !is_paged() ) {
$query->set( 'posts_per_page', $first_page_ppp );
} else {
$paged_offset = $first_page_ppp + ( ($paged - 2) * $ppp );
$query->set( 'offset', $paged_offset );
}
}
}
add_action( 'pre_get_posts', 'home_paged_offset' );
function home_adjust_paged_offset_pagination( $found_posts, $query ) {
$ppp = get_option( 'posts_per_page' );
$first_page_ppp = 7;
$paged = $query->query_vars[ 'paged' ];
if( $query->is_home() && $query->is_main_query() ) {
if( !is_paged() ) {
return( $found_posts );
} else {
return( $found_posts - ($first_page_ppp - $ppp) );
}
}
return $found_posts;
}
add_filter( 'found_posts', 'home_adjust_paged_offset_pagination', 10, 2 );