Increasing the number of posts per page on category, tag, and archive pages in WordPress

In WordPress, you can set the number of pages to display per page in the Administration settings. This is set in Admin/Settings/Reading.

The issue with this is that this is a global setting. If you would like to set a different number of posts to display on the front page than on the category page, you can do it by adding this code to functions.php in your theme.

The following code displays 20 posts per page on all pages that are not the front page (index). This includes search results, categories, tag pages, archives, and author listings.

function change_number_of_posts($query) {
	if ( ! is_front_page()) // Make sure it is not the front page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

You can change this only for category pages for example with this code.

function change_number_of_posts($query) {
	if ( $query->is_category ) // Make sure it is a category page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

The result is that you can set the global setting in the WordPress admin page to 10 posts, which affects the front page (index). You can then insert this code into functions.php and all other pages will display 20 posts.

Note: You do not have to modify your theme other than functions.php for this to work.

Leave a Reply

Consulting

I'm currently available
for Lotus Notes / Domino consulting engagements.

LinkedIn

Connect with me:

LinkedIn

Advertisement
Advertisement
Categories