Archived topic
Showing 20 results from Page 2 onwards
11 replies · Started by Shrawan on May 1, 2022
Hello team,
Can you tell me how I can display 20 results from Page 2 onwards of my blog?
Currently have it configured to show 10 on each page, but I'd like to change it so that I have 10 in the first page, and 20 in the remaining pages.
Thanks,
S
Hi S,
Try this PHP snippet:
function twenty_posts_paged( $query ) {
if ( $query->is_paged() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 20 );
}
}
add_action( 'pre_get_posts', 'twenty_posts_paged' );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Hi Ying,
My developer says adding this code shows 20 on all pages. We only need from the second page, the front page can be 10 recent posts as it is now.
Hi there,
can you add the code? So we can see whats going on
Hi David,
We used this code, and we did see 20 entries from the second page.
However 10 posts between the last post on Page 1 and the first entry on Page 2 don't appear. We followed the order on the Published posts on WP backend and it didn't match with what was shown on Page 2, which is how we figured that 10 posts were missing. We don't observe this problem when moving from Page 2 to Page 3.
Secondly you will also see that there are 51 pages shown in the Pagination of the homepage, and when you click on 51, it's an Error 404. This is also occurring after we inserted the code. How do we fix this?
Lastly on Category pages, we see that Page 3 appears, and it leads to a 404 Error. I'm dropping the link in the Private tab for your perusal.
Hi there,
Try the below snippet instead:
add_action( 'pre_get_posts', 'sk_query_offset', 1 );
function sk_query_offset( $query ) {
// Before anything else, make sure this is the right query...
if ( is_admin() || ! ( $query->is_home() || $query->is_main_query() ) ) {
return;
}
// First, define your desired offset...
$offset = -10;
// Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option( 'posts_per_page' );
// Next, detect and handle pagination...
if ( $query->is_paged ) {
// Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
// Apply adjust page offset
$query->set( 'offset', $page_offset );
}
else {
// This is the first page. Set a different number for posts per page
$query->set( 'posts_per_page', $offset + $ppp );
}
}
Then, in the Reading settings (Tools > Reading), set the blog pages to show at most to 20.
Hi Ying,
This works, but I still see 51 pages in the Pagination.
Also Page 3 appears on Category pages when there are no entries. How do we fix these?
Hi Shrawan,
Messing with the posts_per_page through code is very tricky. You’ll need to consider offsetting posts and the logic will vary depending on how many posts you're displaying per page.
WordPress has an article about it here: https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
You can try something like this:
function itsme_category_offset( $query ) {
$ppp = get_option( 'posts_per_page' );
$first_page_ppp = $query->query_vars[ 'paged' ];
$current_page = get_query_var('paged');
$paged = 20;
if( $query->is_home() && $query->is_main_query() ) {
if( is_paged() ) {
$query->set( 'posts_per_page', $paged );
$paged_offset = $ppp + ( $paged * ( $current_page - 2 ) );
if($paged_offset < 0) {
$paged_offset = $ppp;
}
$query->set('offset', $paged_offset );
}
}
}
add_action( 'pre_get_posts', 'itsme_category_offset' );
function itsme_adjust_category_offset_pagination( $found_posts, $query ) {
$ppp = get_option( 'posts_per_page' );
$first_page_ppp = $query->query_vars[ 'paged' ];
if( $query->is_home() && $query->is_main_query() ) {
if( is_paged() ) {
return( $found_posts + $ppp );
} else {
return( $found_posts/2 );
}
}
return $found_posts;
}
add_filter( 'found_posts', 'itsme_adjust_category_offset_pagination', 10, 2 );
Not sure if this will work but you’ll need to figure out the logic in the code to display it the way you want to.
Hope this helps!
Hi!
Thank you for the reply. We tried the above code but it shows 20 Posts on all Pages, and Page 3 on Category pages when there wasn't one. We've disabled the snippet and set the value for Blog Pages to 10 in Tools->Reading.
Would you be able to give us the right code to add which will
1) Show 10 posts on the homepage, and 20 from the second page onwards
2) 10 posts on all category pages (like our default setting)
3) Show the right number of pages on the homepage, and right number of pages in Category pages in Pagination.
Would be grateful if you can prepare a code for these requirements that solves the problem once and for all.
Thanks,
S
I modified the code above so that it works only in the Blog page. Tested it as well and it’s working as expected from my end, showing 10 on the first page, and 20 on the others. The pagination is correct as well.
Make sure Posts per page is set to 10 in Settings > Reading. Remove all previous code you have as well to alter the pagination and posts per page.
Hope this clarifies!
It works brilliantly Fernando, thank you!
You’re welcome Shrawan!