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

Home Forums Support [Resolved] Blog add-on: Different number of post in first page of blog.

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

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #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.

    #372928
    Tom
    Lead Developer
    Lead Developer

    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.

    #372985
    Morten

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

    #372998
    Tom
    Lead Developer
    Lead Developer

    pre_get_posts is an awesome action πŸ™‚

    Glad I could help! πŸ™‚

    #825419
    Marco

    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.

    #825497
    David
    Staff
    Customer Support

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

    #1323828
    Alessandro

    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!

    #1324348
    Tom
    Lead Developer
    Lead Developer

    Pagination 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/

    #1324607
    Alessandro

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

    #1325308
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

    #1412320
    Alessandro

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

    #1412503
    David
    Staff
    Customer Support

    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.

    #1446488
    Conor

    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.

    #1446567
    Leo
    Staff
    Customer Support

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

Viewing 14 posts - 1 through 14 (of 14 total)
  • The topic ‘Blog add-on: Different number of post in first page of blog.’ is closed to new replies.