Generally, we set the number of articles displayed on each page in the setting → reading → blog page display at most. If we want to customize the display number of some pages, it is not controlled by this, which can be realized through the following code.
1.Add the following code to the current topic function template functions In PHP:
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_query’] ) && ( $query->is_search() ) ) {
$query->set( ‘posts_per_page’, 3 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( $query->is_archive() ) ) {
$query->set( ‘posts_per_page’, 5 );
}
return $query;
}
The final effect of the above code is that the search result page displays 3 articles and the article archive page displays 5 articles.
2.If you want to display different numbers of articles in different categories, change it slightly:
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(1,2)) ) ) {
$query->set( ‘posts_per_page’, 3 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(3,4)) ) ) {
$query->set( ‘posts_per_page’, 5 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(5,6)) ) ) {
$query->set( ‘posts_per_page’, 2 );
}
}
Modify the category ID to display different numbers of articles according to the specified category