[Resolved] Custom number of posts without breaking pagination

Home Forums Support [Resolved] Custom number of posts without breaking pagination

Home Forums Support Custom number of posts without breaking pagination

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1575502
    Ingrid

    Hi Everyone

    I have been trying to fix an issue for days but didn’t manage to figure this out.

    I am using the 2 columns layout + one sticky post on the top. If I had 8 posts showing in each page it would leave one post alone in the last column of the homepage so I set the post per page in the settings as 7 and I am using a code to leave 8 posts in all other pages that doesn’t have a sticky post, so the pages with a sticky post are like this and the other pages are like this

    This is the code I am using:

    function wpsites_query( $query ) {
    if ( $query->is_paged() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', 8 );
        }
    }
    add_action( 'pre_get_posts', 'wpsites_query' );
    

    It works fine and solved the issue with the amount of posts to show but the new problem I got is that the pagination does not count the number of pages correctly, for example in the homepage it shows < 1-2..29> and in the other pages it shows <1-2..26>, the page with the sticky post is showing more pages than the site actually has, therefore I get 404 not found error when clicking on the last page.

    The quick fix I found for now is to hide the last pagination page number by CSS but that is not ideal

    Thanks in advance!

    #1576158
    David
    Staff
    Customer Support

    Hi there,

    See Tom’s response to what a pain navigation can be when the post count is changed:

    https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-1324348

    You may also be interested in his method of apply the different post counts:

    https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-372928

    #1581061
    Ingrid

    Thanks David
    I tried the code from the first link and but unfortunately the problem still persists, the pagination is still showing a higher number of pages in the first page.

    #1582012
    Tom
    Lead Developer
    Lead Developer

    That code should still work: https://sridharkatakam.com/changing-posts-per-page-first-page-without-breaking-pagination-wordpress/

    It should replace the code you shared above as well. Is it not doing anything at all?

    #1583049
    Ingrid

    I replaced my code with this one and it made the homepage show one less post but it doesn’t solve the issue with the pagination. Already cleaned the cache etc but still shows a wrong number of pages in the pagination.

    #1583386
    Tom
    Lead Developer
    Lead Developer

    Hmm, I found this: https://wordpress.stackexchange.com/questions/324836/using-the-posts-pagination-with-offset-adds-extra-empty-page

    So instead of the second function you’re using (there are two separate functions in the thread we linked to), try this one:

    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 3;
    
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    Just change $first_page_ppp to the correct number of posts on the first page.

    #1589082
    Ingrid

    Unfortunately this code didn’t work for me, it still shows the amount of posts I have in the ‘reading’ settings in all pages.
    The code did some change in the pagination, now is showing 1.2…10 and in the next page 1.2…27 not completely correct but at least in the first page isn’t showing anymore more pages than the site actually has.

    If this is too hard to fix maybe I will just keep hiding the last pagination page number by CSS for now

    #1589750
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, you still need both functions:

    function wpsites_query( $query ) {
        if ( $query->is_paged() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', 8 );
        }
    }
    add_action( 'pre_get_posts', 'wpsites_query' );
    
    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 8;
    
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    This is something I wish WordPress had built-in support for, it’s certainly far too complex to achieve.

    #1619677
    Ingrid

    Hi! Sorry for the late reply, that didn’t solve the issue either so for now I will just keep using CSS to hide the last navigation button. Thanks a lot!

    #1620458
    Tom
    Lead Developer
    Lead Developer

    No problem!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.