- This topic has 9 replies, 3 voices, and was last updated 3 years, 3 months ago by Tom.
-
AuthorPosts
-
December 11, 2020 at 1:05 am #1575502Ingrid
Hi Everyone
I have been trying to fix an issue for days but didn’t manage to figure this out.
I am using the 2 columns layout + one sticky post on the top. If I had 8 posts showing in each page it would leave one post alone in the last column of the homepage so I set the post per page in the settings as 7 and I am using a code to leave 8 posts in all other pages that doesn’t have a sticky post, so the pages with a sticky post are like this and the other pages are like this
This is the code I am using:
function wpsites_query( $query ) { if ( $query->is_paged() && $query->is_main_query() ) { $query->set( 'posts_per_page', 8 ); } } add_action( 'pre_get_posts', 'wpsites_query' );
It works fine and solved the issue with the amount of posts to show but the new problem I got is that the pagination does not count the number of pages correctly, for example in the homepage it shows < 1-2..29> and in the other pages it shows <1-2..26>, the page with the sticky post is showing more pages than the site actually has, therefore I get 404 not found error when clicking on the last page.
The quick fix I found for now is to hide the last pagination page number by CSS but that is not ideal
Thanks in advance!
December 11, 2020 at 6:56 am #1576158DavidStaffCustomer SupportHi there,
See Tom’s response to what a pain navigation can be when the post count is changed:
You may also be interested in his method of apply the different post counts:
December 15, 2020 at 12:48 am #1581061IngridThanks David
I tried the code from the first link and but unfortunately the problem still persists, the pagination is still showing a higher number of pages in the first page.December 15, 2020 at 12:35 pm #1582012TomLead DeveloperLead DeveloperThat code should still work: https://sridharkatakam.com/changing-posts-per-page-first-page-without-breaking-pagination-wordpress/
It should replace the code you shared above as well. Is it not doing anything at all?
December 16, 2020 at 7:08 am #1583049IngridI replaced my code with this one and it made the homepage show one less post but it doesn’t solve the issue with the pagination. Already cleaned the cache etc but still shows a wrong number of pages in the pagination.
December 16, 2020 at 11:02 am #1583386TomLead DeveloperLead DeveloperHmm, I found this: https://wordpress.stackexchange.com/questions/324836/using-the-posts-pagination-with-offset-adds-extra-empty-page
So instead of the second function you’re using (there are two separate functions in the thread we linked to), try this one:
function my_offset_pagination( $found_posts, $query ) { $ppp = get_option( 'posts_per_page' ); $first_page_ppp = 3; if( $query->is_home() && $query->is_main_query() ) { if( !is_paged() ) { return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp ); } else { return( $found_posts - ($first_page_ppp - $ppp) ); } } return $found_posts; } add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );
Just change
$first_page_ppp
to the correct number of posts on the first page.December 21, 2020 at 5:14 am #1589082IngridUnfortunately this code didn’t work for me, it still shows the amount of posts I have in the ‘reading’ settings in all pages.
The code did some change in the pagination, now is showing 1.2…10 and in the next page 1.2…27 not completely correct but at least in the first page isn’t showing anymore more pages than the site actually has.If this is too hard to fix maybe I will just keep hiding the last pagination page number by CSS for now
December 21, 2020 at 11:08 am #1589750TomLead DeveloperLead DeveloperJust to confirm, you still need both functions:
function wpsites_query( $query ) { if ( $query->is_paged() && $query->is_main_query() ) { $query->set( 'posts_per_page', 8 ); } } add_action( 'pre_get_posts', 'wpsites_query' ); function my_offset_pagination( $found_posts, $query ) { $ppp = get_option( 'posts_per_page' ); $first_page_ppp = 8; if( $query->is_home() && $query->is_main_query() ) { if( !is_paged() ) { return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp ); } else { return( $found_posts - ($first_page_ppp - $ppp) ); } } return $found_posts; } add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );
This is something I wish WordPress had built-in support for, it’s certainly far too complex to achieve.
January 15, 2021 at 2:46 am #1619677IngridHi! Sorry for the late reply, that didn’t solve the issue either so for now I will just keep using CSS to hide the last navigation button. Thanks a lot!
January 15, 2021 at 11:10 am #1620458TomLead DeveloperLead DeveloperNo problem!
-
AuthorPosts
- You must be logged in to reply to this topic.