也来自codex.wordpress.com/Pagination (根据“Advanced Troubleshooting Steps“>”Removing query_posts from the main loop)关于通过pre_get_posts
米洛提到的行动:
[…] 将主页和类别页的查询添加回主题的功能中。php文件:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it\'s the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set(\'posts_per_page\', 3);
}
if(is_category()){
$query->set(\'posts_per_page\', 3);
}
}
}
add_action( \'pre_get_posts\', \'my_post_queries\' );
因此,您的实现可能看起来像
function modify_query( $query ) {
if( !$query->is_main_query() )
return; //Only wish to modify the main query
//Modify the query vars for the home and front pages
if( is_home() || is_front_page() ) {
$paged = get_query_var(\'page\');
//If the \'page\' query var isn\'t set, or didn\'t return an integer, default to 1
$paged = !isset( $paged ) || !is_int( $paged ) ? $paged : 1;
$query->set( \'paged\', $paged );
$query->set( \'posts_per_page\', 2 );
}
}
add_action( \'pre_get_posts\', \'modify_query\' );