How to change the number of posts displayed on category pages in WordPress

To change the number of posts displayed on category pages in WordPress, you can use the pre_get_posts action hook to modify the posts_per_page property of the query. Add the following code snippet to your theme’s functions.php file:

  1. In your WordPress admin dashboard, go to “Appearance” > “Theme Editor.”
  2. In the right sidebar, locate and click on the “functions.php” file.
  3. Add the following code snippet at the end of the file:
function my_theme_change_category_posts_per_page($query) {
    if ($query->is_main_query() && !is_admin() && $query->is_category()) {
        $query->set('posts_per_page', 10); // Set the number of posts per page
    }
}
add_action('pre_get_posts', 'my_theme_change_category_posts_per_page');

Replace 10 with the desired number of posts you want to display per page on category pages.

This code snippet creates a function called my_theme_change_category_posts_per_page() that checks if the current query is the main query, if it’s not in the admin area, and if it’s a category page. If the condition is met, the number of posts per page is set using the $query->set() method.

  1. Click “Update File” to save the changes.

Now, your category pages will display the specified number of posts per page. This change will not affect other archive pages or the main blog page.

Leave a Reply

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