如果你不介意打电话的费用COUNT
为了首先获得帖子总数(你有一个动态首页,以帖子数量为基础,因此它不适用于简单的偏移量),我写了一些东西(时间很晚,我很累,所以可能有一些bug/可能不是正确的解决方案):
// This code can be placed in the \'functions.php\' file.
function wt_posts_clauses_hook( $clauses, $query ) {
if ( $query->is_main_query() && $query->is_home() ) {
global $wpdb;
extract( $clauses );
if ( ! empty( $groupby ) ) $groupby = \'GROUP BY \' . $groupby;
$sql = "SELECT COUNT(ID) FROM $wpdb->posts $join WHERE 1=1 $where $groupby";
$no_posts = $wpdb->get_var( $sql );
$posts_per_page = get_option( \'posts_per_page\' );
$extra = $no_posts % $posts_per_page;
$paged = absint( $query->get( \'paged\' ) );
if ( ! $paged ) $paged = 1;
$from = ( $paged - 1 ) * $posts_per_page;
$to = $posts_per_page;
if ( $extra ) {
if ( $paged < 2 ) {
$from = 0;
$to = $extra;
} else {
$from = $posts_per_page * ( $paged - 2 ) + $extra;
}
}
$clauses[\'limits\'] = "LIMIT {$from}, {$to}";
}
return $clauses;
}
add_filter( \'posts_clauses_request\', \'wt_posts_clauses_hook\', 99, 2 );
如果不想在每个列表页调用上运行缓存机制,可以通过读取
found_posts
所有物如果它与缓存项不对应,只需删除缓存即可。当然,您首先必须修改上面的此函数,以便最初尝试并读取
$no_posts
缓存中的值。如果没有,则运行
$sql
使用相应的
$no_posts
价值