Support » Theme: Astra » Limit number of posts on home/front page ONLY

  • I’d like to limit the number of posts on my home page to 2, but have the archives, categories, pagination pages all set to 10.

    Is there a way to do this within functions.php? That way if I change my mind I can just edit the numbers inside of functions.php file. It looks like something like this that I found in another theme’s support forum, but it doesn’t work in astra.

    function theme_custom_post_limit( $limit, $query ) {
    
        if (  $query->is_main_query() &&  $query->is_home()  ){
            return 'LIMIT 0';
        }
    
        return $limit;
    }
    
    add_filter( 'post_limits', 'theme_custom_post_limit', 10, 2 );

    Cheers,
    Lori

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @delphine,

    To change the number of posts that will be displayed on the homepage, you can go to Settings > Reading then change the Blog pages show at most field to any number you like.

    And to change the number of posts on the archive page, please try adding this custom PHP code to your site

    add_filter('pre_get_posts', 'limit_archive_posts');
    function limit_archive_posts($query){
        if ($query->is_archive) {
            $query->set('posts_per_page', 10);
        }
        return $query;
    }

    The code above will show ten posts. You can just change the 10 number in the code to any number you like. And please refer to this doc to know how to add custom PHP code in Astra.
    
    I hope that will help.

    Kind regards,
    Herman 🙂

    Thread Starter Lori

    (@delphine)

    Hi Herman, thanks!
    I tried this, and ran into a couple of problems. I’ll describe them below. But maybe a simpler solution would be a plugin. There is a plugin called Custom Post Limits that does everything I could possibly want to do. https://wordpress.org/plugins/custom-post-limits/

    Do you know if there are any compatibility issues with Astra? I’ve installed it and it seems to be working.

    Here are the problems that I had with the code you gave me:

    1) This also limits the posts I see in the back end if I filter them by category. For example, if I have 40 posts with the category “podcast” and filter to see just those, I only see ten of them and there are no pagination links to let me access the others. I also tried editing the code to target specific categories instead of all of the archives, and only those targeted categories got messed up in the backend.

    2) The pages of previous and past posts that are linked at the bottom of the home page are limited to whatever I set for the home page. That is, if I set it to 3 posts, then all those pages only have three posts each. This creates a LOT of pages to be cached.

    Hi @delphine,

    Yes, for some cases using a plugin could be a simpler solution. However, please bear in mind that with so many plugins in the market, it is not possible for us to test each plugin with Astra. But, Astra is a well-coded base WordPress theme following the Theme Development Standards which means it should be compatible with most WordPress plugins.

    Apart from that, if you still want to use custom code for this, you might want to refer to this article.
    
    I hope it helps.

    Kind regards,
    Herman 🙂

    P.S. We recommend reaching out to us through our Support Portal for faster and quicker help and resolution as mentioned here.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Limit number of posts on home/front page ONLY’ is closed to new replies.