- This topic has 6 replies, 3 voices, and was last updated 5 months, 2 weeks ago by
Fernando.
-
AuthorPosts
-
October 13, 2022 at 2:29 am #2372064
Fabien
Hi,
I am using the “Make First Post Featured” (width: 100%) in order to feature the latest post on archive pages.
By doing this, the grid below is not complete (12 posts – 1 post featured = 11 posts / 3 columns = not an interger). Then the last space of the grid is empty.
Thus, I’ve changed the number of post on the first page of archives using the following function :
/** * Archive - Changing the number of posts on page 1. */ add_action( 'pre_get_posts', function ( $query ) { if ( ! $query->is_main_query() ) { return; } if ( ( ( ! $query->is_paged ) && ( !is_admin() ) ) && ( ( is_category() ) || ( is_home() ) ) ) { $query->set( 'posts_per_page', 13 ); } }, 1 );
It works well. However, the last post displayed is also displayed on the top of page 2.
Any idea on how to fix this ?
October 13, 2022 at 2:42 am #2372073David
StaffCustomer SupportHi there,
See Fernandos reply here:
https://generatepress.com/forums/topic/different-number-of-posts-on-first-page/#post-2171894
That PHP Snippet will offset the number of posts on Paged archives.
In the code you will see 2 x instances of: $offset = -3; those will need changing to
-1
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 13, 2022 at 4:48 am #2372174Fabien
Hi @david,
Thanks for the first reply.
The snippet provided works but :
- Offset needs to be set to
-2
in order to have a completed grid on the first page - The first page displays only 10 posts (1 featured + 9 posts). My goal is to have 13 posts (1 featured + 12 posts)
Any idead on how to achieve this ?
October 13, 2022 at 2:07 pm #2372765Fabien
Aslo something I’ve noticed, when using Fernandos code with this conditional tags :
// Before anything else, make sure this is the right query... if ( ! $query->is_home() ) { return; }
The issue doesn’t appear with other conditional tags such as :
// Before anything else, make sure this is the right query... if ( ! $query->is_category() ) { return; }
It breaks everything done with GP Elements Blocks.
Can you please try on your end ? Seems like a bug IMO.
October 13, 2022 at 6:11 pm #2372871Fernando Customer Support
Hi Fabien,
To clarify, how many posts do you want displayed in page 1, and on other pages?
Moreover, will this be applied to just the category archive pages or also in your blog or other archive pages?
October 14, 2022 at 12:15 am #2373052Fabien
Hi Fernando,
What I would like to achieve:
- 13 posts on the first page, 12 posts on the other pages
- Targeting the categories pages (is_category) and the home page of the blog (is_home)
Thanks,
Fabien
October 14, 2022 at 12:31 am #2373070Fernando Customer Support
Go to Settings > Reading and set the
Blog pages show at most
to13
.Then, remove the snippets you have for this, and add this instead:
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 = 12; if( !is_admin() && ( $query->is_home() || $query->is_category() ) && $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( !is_admin() && ( $query->is_home() || $query->is_category() ) && $query->is_main_query() ) { if( is_paged() ) { return( $found_posts + 1 ); } else { return( $found_posts - 1 ); } } return $found_posts; } add_filter( 'found_posts', 'itsme_adjust_category_offset_pagination', 10, 2 );
- Offset needs to be set to
-
AuthorPosts
- You must be logged in to reply to this topic.