Archived topic
9 blog items on home, 10 on the next pages
17 replies · Started by jmarc on November 19, 2022
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 much
JMarc
Hi 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;
}
Hello Ying
It works fine !
Thank you very much
Regards
You are welcome :)
..
I updated the code here: https://generatepress.com/forums/topic/9-blog-items-on-home-10-on-the-next-pages/#post-2424514
Hi Ying
got it thanks :)
JM
PS : double authentification to get into this forum is a real Pain in the a**
Lol...I have to do the same thing every day to start my work...
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
JM
Hi there,
edit this line:
function sk_query_offset( &$query ) {
And change it to:
function sk_query_offset( $query ) {
ie. remove the & ampersand.
Hi David
thank you but that doesn't fix it ...
JM
I 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!
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
it seems that .tablenav-pages do not appear (at the bottom of ../wp-admin/edit.php)
I 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.