- This topic has 17 replies, 3 voices, and was last updated 3 years, 4 months ago by
Ying.
-
AuthorPosts
-
November 19, 2022 at 1:25 pm #2423343
jmarc
Hello,
I made a new post from an old one created by Joe ( https://generatepress.com/forums/topic/9-blog-items-on-home-10-on-following-pages/#post-2423342 )
Is it possible to get 9 blog posts on the homepage and 10 on the following pages? It seems a bit odd when using the standard setup with the big featured post on the homepage.
So I use the code given by David for my site but pagination is not working fine for the last page (I have one or two pages more and when cliking I have an Error 404)
Can you please take a look ?
Thank you very muchJMarc
November 20, 2022 at 1:35 pm #2424514Ying
StaffCustomer SupportHi there,
Can you try this Snippet instead:
add_action( 'pre_get_posts', 'sk_query_offset', 1 ); function sk_query_offset( $query ) { if ( is_admin() ) { return; } // Before anything else, make sure this is the right query... if ( ! ( is_main_query() ) ) { return; } // Don't do anything to Elements. if ( 'gp_elements' === $query->get( 'post_type' ) ) { return; } else { // First, define your desired offset... $offset = -1; // 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 ); } } } add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 ); function sk_adjust_offset_pagination( $found_posts, $query ) { // Define our offset again... $offset = -1; //get posts per page from WP settings $ppp = get_option( 'posts_per_page' ); $real_page_numbers =ceil(($found_posts - $ppp - $offset) / $ppp) + 1; // Ensure we're modifying the right query object... if ( is_main_query() && ! is_paged() && ! is_admin()) { // Reduce WordPress's found_posts count by the offset... return $real_page_numbers * ($ppp + $offset); } return $real_page_numbers * $ppp; }November 21, 2022 at 12:05 am #2424863jmarc
Hello Ying
It works fine !
Thank you very muchRegards
November 21, 2022 at 10:29 am #2425966Ying
StaffCustomer SupportYou are welcome 🙂
November 22, 2022 at 4:01 am #2426955jmarc
..
November 22, 2022 at 10:00 am #2427706Ying
StaffCustomer SupportI updated the code here: https://generatepress.com/forums/topic/9-blog-items-on-home-10-on-the-next-pages/#post-2424514
November 22, 2022 at 1:05 pm #2427904jmarc
Hi Ying
got it thanks 🙂
JMPS : double authentification to get into this forum is a real Pain in the a**
November 22, 2022 at 4:48 pm #2428087Ying
StaffCustomer SupportLol…I have to do the same thing every day to start my work…
December 2, 2022 at 8:17 am #2446884jmarc
Hi Ying,
since I’ve put this code, pagination in admin doesn’t work anymore….
For instance, at the end of the page …/wp-admin/edit.php there is no pagination !
Can you please help ?Thank you
JMDecember 2, 2022 at 9:14 am #2446976David
StaffCustomer SupportHi there,
edit this line:
function sk_query_offset( &$query ) {And change it to:
function sk_query_offset( $query ) {ie. remove the
&ersand.December 3, 2022 at 12:52 am #2447609jmarc
Hi David
thank you but that doesn’t fix it …
JM
December 3, 2022 at 12:27 pm #2448340Ying
StaffCustomer SupportI just tested the code, it seems working fine.
Can you link us to your site? And can you copy and paste the exact code you are using and wrap it with the CODE tags so we can see if there’s any error in the code?
Let me know!
December 3, 2022 at 12:33 pm #2448345jmarc
Hi Ying
here’s the exact code I use :
add_action( 'pre_get_posts', 'sk_query_offset', 1 ); function sk_query_offset( $query ) { if ( is_admin() ) { return; } // Before anything else, make sure this is the right query... if ( ! ( is_main_query() ) ) { return; } // Don't do anything to Elements. if ( 'gp_elements' === $query->get( 'post_type' ) ) { return; } // First, define your desired offset... $offset = -1; // 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 ); } } add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 ); function sk_adjust_offset_pagination( $found_posts, $query ) { if ( is_admin() ) { return; } // Define our offset again... $offset = -1; //get posts per page from WP settings $ppp = get_option( 'posts_per_page' ); $real_page_numbers =ceil(($found_posts - $ppp - $offset) / $ppp) + 1; // Ensure we're modifying the right query object... if ( is_main_query() && ! is_paged() ) { // Reduce WordPress's found_posts count by the offset... return $real_page_numbers * ($ppp + $offset); } return $real_page_numbers * $ppp; }Thank you
December 3, 2022 at 12:37 pm #2448347jmarc
it seems that .tablenav-pages do not appear (at the bottom of ../wp-admin/edit.php)
December 3, 2022 at 1:00 pm #2448363Ying
StaffCustomer SupportI can not see the admin dashboard though, it will require an admin login.
But I can replicate the issue on my end. I’ll see how to modify the code 🙂
Will get back to you later.
-
AuthorPosts
- You must be logged in to reply to this topic.