Site logo

Archived topic

Blog add-on: Different number of post in first page of blog.

13 replies · Started by Morten on August 24, 2017

Viewing posts 1–14 of 14

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.

Give 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.

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...)

pre_get_posts is an awesome action :)

Glad I could help! :)

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.

Hi there, seen your other topic. We will attend to that asap.

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 page

Gp premium Version 1.10.0

Thank you very much!

WOW so fast and so furious! It works, tahnk you very very very much!

You're welcome :)

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 );
}
}

Hi 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.

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.

I'll answer the questions in your own topic so we can just stick with that one.

This archived topic is closed to new replies.