Archived topic
9 blog items on home, 10 on following pages
10 replies · Started by J. on January 21, 2022
Hi there,
I'm using the 'Volume' template. 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.
Hi there,
you would need to add this PHP Snippet to your site:
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;
// Ensure we're modifying the right query object...
if ( is_main_query() ) {
// Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
This article explains how to add PHP:
I've pasted this in the functions.php from my (clean) child theme, but I see no results... Am I doing something wrong?
Can you share a link to the site ?
Shared in the private info.
Hi Joe,
The code should work, I just tested on my site.
Can you disable all other plugins except GP premium and GB to test?
Hi
I use this code for my site but pagination is not working fine for the last page...
Can you please take a look ?
Thank you
JMarc
PS : how do I send you the URL in private so you can check the site ?
Someone can help please ? Thank you ;)
Please open a new topic so you can use the private URL field:
https://docs.generatepress.com/article/using-the-premium-support-forum/#open-support-topic
https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
Please also be mindful that it's Saturday here so our responses will be slower:
https://generatepress.com/what-support-includes/
Thanks for your understanding :)
I'll do that and I thank you Leo ....
Thanks :)