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 Made for WordPress   /   Ready for your story

Ready to Build?

Your next great website starts here.

Build more websites with one flexible collection—and keep every theme for life.

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

The complete collection

$75

One payment. No renewals.

Get the Themes Bundle — $75