如果我理解正确,您只想在存档中显示不同数量的帖子,因此根本不需要自定义二次查询。您可以通过pre_get_posts
钩子,或利用posts_per_page
或posts_per_archive_page
参数
示例pre_get_posts
function wpse188786_different_number_of_posts_for_archive_ppp( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_archive ) {
$query->set( \'posts_per_page\', 4 );
}
}
}
add_action( \'pre_get_posts\',\'wpse188786_different_number_of_posts_for_archive_ppp\' );
示例
posts_per_archive_page
function wpse188786_different_number_of_posts_for_archive_ppap( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
// no $query->is_archive check needed, because posts_per_archive_page
// does override, when $query->is_archive or $query->is_search is true
$query->set( \'posts_per_archive_page\', 4 );
}
}
add_action( \'pre_get_posts\',\'wpse188786_different_number_of_posts_for_archive_ppap\' );