我升级到WordPress 3.4版本,在第一页上显示的帖子数量与其他页面上显示的帖子数量有所不同,这让我很头疼。第一页仍然显示了5篇文章,但当我转到/page/2/时,我得到了一个404错误。
我在自定义帖子类型页面和作者页面上分页,效果很好。这只会发生在头版,将所有帖子类型组合在一起。
以下是我的代码,可能会有所帮助:
// For adding custom post types to the feed
if ( ( is_front_page() && false == $query->query_vars[\'suppress_filters\'] ) || is_feed() ){
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts( array(\'post_type\'=>array(\'post\', \'gaming\', \'entertainment\', \'tech\', \'breakroom\', \'podcasts\', \'off-grid\'),\'paged\'=>$paged ) );
}
// different amount of post on front page then other pages
$temp = $wp_query;
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query(array(
\'post_type\' => array(\'post\', \'gaming\', \'entertainment\', \'tech\', \'breakroom\', \'podcasts\', \'off-grid\'),
\'post_status\' => \'publish\',
\'paged\' => $paged,
\'posts_per_page\' => 10,
\'caller_get_posts\'=> 1.
));
$max_first_page = 5; // Show this many posts on front page
$args = $wp_query->query;
$posts_per_page = get_query_var(\'posts_per_page\');
$posts_to_skip = $posts_per_page - $max_first_page;
if ($paged == 1) {
$max_posts = $max_first_page;
$firstPageCheck = "yes";
} else {
$max_posts = $posts_per_page;
$args[\'offset\'] = (($paged - 1) * $max_posts)- $posts_to_skip;
$firstPageCheck = "no";
}
query_posts($args);
if ($wp_query->max_num_pages < ceil(($wp_query->found_posts + $posts_to_skip)/$posts_per_page))
++$wp_query->max_num_pages;
$counter = 0;
if ($firstPageCheck == "yes") {
// front page here
} else {
// all other pages
}
最合适的回答,由SO网友:meloniq 整理而成
它停止使用WP 3.4,因为有处理\\u 404()的修复程序,并且您在主页上使用了错误的方式。。。修改主查询而不是创建新查询。
变更集:http://core.trac.wordpress.org/changeset/19892
Sample code:
function my_query_for_homepage( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( \'post_type\', array( \'post\', \'gaming\', \'entertainment\', \'tech\', \'breakroom\', \'podcasts\', \'off-grid\' ) );
}
}
add_action( \'pre_get_posts\', \'my_query_for_homepage\' );