Show Different Number Of Posts Per Category In WordPress

Assume that we want to show 5 posts per page for category “WordPress” and 10 posts per page for category “News”, just open the functions.php file in your theme folder and insert these line into it:

add_action('pre_get_posts', 'diff_post_count_per_cat');
 
function diff_post_count_per_cat() {
    if (is_admin()) return;
 
    $cat = get_query_var('category_name');
    switch ($cat) {
        case 'wordpress':
            set_query_var('posts_per_page', 5);
            break;
        case 'wordpress/news':
            set_query_var('posts_per_page', 2);
            break;   
    }
}

Change “wordpress” and “news” into real category names of your blog. Note that in the code above, we use category slug, not original name. If the category is a child category, don’t forget to insert full path (i.e. “wordpress/news” in the example below).

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.