I'm attempting to display a total number of posts for main categories in WordPress instead of displaying all the posts.
How do I go about limiting the total number of posts displayed without pagination?
The following doesn't work:
function target_main_category_query_with_conditional_tags( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( is_category(array( 45, 49 )) ) {
$query->set( 'posts_per_page', 9 );
$query->set( 'nopaging', true );
}
}
}
add_action( 'pre_get_posts', 'target_main_category_query_with_conditional_tags' );
This code manipulates the category page.
Conditions:
! is_admin(): A visitor without the admin role$query->is_category(): Category page$query->get_queried_object()->parent == 0: The parent categoryNote: as @Karl Hill said,
nopagingparameter overridesposts_per_page, do not use it.