How to Change the Number of Posts on Category Pages in WordPress

By ThemesDNA1 min read

Share:

In WordPress, there is a setting to change the number of posts listed on blog pages (on the non-static homepage, category pages, tag pages, other archive pages and search results pages). It is called "Blog pages show at most" and located at "WordPress Dashboard" -> "Settings" -> "Reading".

But what if you want to display a different number of posts on category pages than on other blog pages? Let's say you want to display 20 posts on category pages. You can achieve this by adding the below PHP code to the functions.php file of your theme:

function themesdna_number_of_posts_on_category_pages( $query ) {
    if( $query->is_main_query() && ! is_admin() && ( $query->is_category() ) ) {
        $query->set( 'posts_per_page', '20' );
    }
}
add_action( 'pre_get_posts', 'themesdna_number_of_posts_on_category_pages' );

Note: Replace the value "20" with the number of posts you want to display on category pages.

Premium WordPress themes One-time payment   /   Lifetime updates

Ready to Build?

Get the ThemesDNA bundle.

Get all premium ThemesDNA themes with lifetime updates and unlimited website usage for a one-time payment.

  • 56 premium themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete premium theme bundle

$75

One payment. No renewals.

Get the ThemesDNA Bundle — $75