18 Apr 2010

WordPress: Limit posts per page

Choose and add the following lines to your WordPress Theme’s template functions.php file (For example, functions.php file for a Theme named “default” would probably reside in the directory wp-content/themes/default/functions.php):

  • function limit_posts_per_page() {
    	if ( is_category() )
    		return 2;
    	else
    		return 5; // default: 5 posts per page
    }
    
    add_filter('pre_option_posts_per_page', 'limit_posts_per_page');
    
  • function limit_posts_per_archive_page() {
    	if ( is_category() )
    		set_query_var('posts_per_archive_page', 2); // or use variable key: posts_per_page
    }
    
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');
    

The code example above is for showing 2 posts per page when any Category archive page is being displayed. Same results in two different way.

For more possible page conditions, please refer to the Conditional Tags page in the WordPress Codex.