Categories
CMS Mastering Development Wordpress

Limit posts only on home page – ideally in functions.php

I’d like to restrict the number of posts that show on my home page to 3, but have all the other archive, category, pagination pages, etc. at whatever number I set in the "reading" settings in the WordPress dashboard.

Is there a nice way to do this in functions.php?

I tried the following, which does limit the posts on the home page to 3, but it also creates pagination links to pages with 3 posts each.

    add_action( 'pre_get_posts',  'change_posts_number_home_page'  );
function change_posts_number_home_page( $query ) {

if ($query->is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 3 );

return $query;
}

Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *