Number of post for a WordPress archive page

I want to be able to change the amount of post shown on each archive page on a per category basis. Is there something I can change in the loop code or a plugin that gives me that ability?

Thank you SE community.

3 s
3

paste this in your theme function.php file

add_filter('pre_get_posts', 'Per_category_basis');

function Per_category_basis($query){
    if ($query->is_category) {
        // category named 'books' show 12 posts
        if (is_category('books'){
            $query->set('posts_per_page', 12);
        }
        // category With ID = 32 show only 5 posts
        if (is_category('32'){
            $query->set('posts_per_page', 5);
        }
    }
    return $query;

}

explanation:
first we check if its actually a category because we don’t want to change anything else.
if so we check to see and change the number of posts to show, on the first one we check by category name ‘books’ and assign the value of 12 posts_per_page the second one is by category id of ’32’ and we assign the value of 5 posts_per_page just to show you that you can use either one.
and you can just add as many checks and assignments.

Leave a Comment