- This topic has 13 replies, 5 voices, and was last updated 3 years, 2 months ago by
Leo.
-
AuthorPosts
-
August 24, 2017 at 4:27 am #372554
Morten
What if I want equal number of post to show on every page except on the first page. Blog setup is two-column with one full-width post on the top, and the number of post to show is set to 3,5,7 etc.
The next pages also shows unequal numbers – 3,5, etc. I want them to be 2,4,6 from second page on, not to leave a gap on the bottom of the pages.
August 24, 2017 at 1:10 pm #372928Tom
Lead DeveloperLead DeveloperGive this function a try:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 ); function tu_change_posts_per_page( &$query ) { if ( ! is_main_query() ) { return; } if ( ! $query->is_paged ) { $query->set( 'posts_per_page', 10 ); } }
Update the 10 value to the number of posts you want to show on the first page.
August 24, 2017 at 2:38 pm #372985Morten
Thank you! Brilliant support and help far beyond what is expected of you. As always!
( And your solution looks so simple π I know a bit of php and have “a working knowledge” of WP. But I spent hours trying to solve this. With zero results…)August 24, 2017 at 3:09 pm #372998Tom
Lead DeveloperLead Developerpre_get_posts is an awesome action π
Glad I could help! π
March 1, 2019 at 3:43 am #825419Marco
this is not working for me – I’d like to e.g. have 7 posts on the first page incl. one featured and then e.g. 6 on the following pages.
March 1, 2019 at 5:52 am #825497David
StaffCustomer SupportHi there, seen your other topic. We will attend to that asap.
June 11, 2020 at 8:07 am #1323828Alessandro
Hello, it’s working to me!
However the last post of the first page is the first post of the second page. Is there any solution?
first page first blog page
second page second blog pageGp premium Version 1.10.0
Thank you very much!
June 11, 2020 at 4:33 pm #1324348Tom
Lead DeveloperLead DeveloperPagination can be tricky with this sort of thing. This article might help: https://sridharkatakam.com/changing-posts-per-page-first-page-without-breaking-pagination-wordpress/
June 12, 2020 at 12:30 am #1324607Alessandro
WOW so fast and so furious! It works, tahnk you very very very much!
June 12, 2020 at 9:25 am #1325308Tom
Lead DeveloperLead DeveloperYou’re welcome π
August 21, 2020 at 3:26 am #1412320Alessandro
Ehm it works to me, but if i set the number `posts_per_page’, 7 , the posts list in the administrator panel shows just 7 posts of a single author. And strange things happen with the list of all posts, like making disappear some posts and duplicate some other.
Any ideas? THANK YOU!add_action( ‘pre_get_posts’, ‘tu_change_posts_per_page’, 1 );
function tu_change_posts_per_page( &$query ) {
if ( ! is_main_query() ) {
return;
}if ( ! $query->is_paged ) {
$query->set( ‘posts_per_page’, 7 );
}
}August 21, 2020 at 5:50 am #1412503David
StaffCustomer SupportHi there,
try changing:
if ( ! is_main_query() ) {
to:
if ( ! is_main_query() || is_admin() ) {
This should return the default posts per page for the admin area.
September 16, 2020 at 5:34 pm #1446488Conor
I would change the 1 to a 0 because 0 is the first page. Also, when you run the snippet, just make it run on the front-end only. Or does this cause some other issue I am unaware of?
The only problem I am still having is that I set the number of posts on page 0 to 6, but if I have more than 6 stickied posts, then it will show more that 6 posts on the first page.
September 16, 2020 at 8:08 pm #1446567Leo
StaffCustomer SupportI’ll answer the questions in your own topic so we can just stick with that one.
-
AuthorPosts
- The topic ‘Blog add-on: Different number of post in first page of blog.’ is closed to new replies.