Black Friday Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] How to change post count on blog home page vs others

Home Forums Support How to change post count on blog home page vs others

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #452644
    Scott

    Hi

    I would like the differ the count of posts shown on the home page of my blog vs other page i.e on the home page ( navigation page 1 ) i would like 3 posts shown but when i navigate to page 1,2..99 via the pagination i want there to be 4 posts show. I tried this with hooks but so far it’s failing.

    add_filter('pre_get_posts', 'posts_on_page', 1);
    
    function posts_on_page($query){
        if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) {
          $query->set( 'posts_per_page', 3 );
        }else{
          $query->set( 'posts_per_page', 4 );
        }
    }

    It continues to show the post per page configured in “Settings->Reading->Blog pages show at most”

    Can someone help please ?

    thanks
    Scott

    GP Premium 1.5.6
    #452649
    Tom
    Lead Developer
    Lead Developer

    That code should make it so the main blog page (set in Settings > Reading) only displays 3 posts.

    You would likely need check which page you’re on. For example: https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-372928

    #452684
    Scott

    ok thanks but it still doesn’t work for me. I tried this in the ‘Inside Content Container’ hook

    
    <?php
    
    function posts_on_page( $query ) {
        if ( ! is_main_query() ) {
            return;
        }
    
        if ( ! $query->is_paged ) {
            $query->set( 'posts_per_page', 10 );
        }
    }
    
    add_action( 'pre_get_posts', 'posts_on_page', 1 );
    
    ?>
    

    The post count remains the same Settings > Reading

    #452697
    Scott

    I used the snippets plugin instead of the hooks and it worked better. will resolve thanks.

    #453140
    Tom
    Lead Developer
    Lead Developer
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.