Use this code in your child themes functions file with a conditional tag to target a specific archive page type to set posts per page.
add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
function set_posts_per_archive_page( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
$query->set('posts_per_page', '5');
}
}
The above code sets all archive pages to display 5 posts per page.
Post Per Page For Specific Category #
Only executes on the news category archive page.
add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
function set_posts_per_archive_page( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_category( 'news' ) ) {
$query->set('posts_per_page', '1');
}
}
Swap out the slug news in the above code to target a different category.
Swap out the 1 to change the number of posts per page you want to display on the archive page.
Leave a Reply