我试图做一些与这个问题非常相似的事情:Have different number of posts on first page
唯一的区别是我需要在博客的第一页上发表10篇文章,每隔一页发表9篇文章。
我有以下设置:
帖子显示在帖子页面(/新闻/)上。php用于博客页面模板,它使用标准while循环来显示帖子我将“设置”>“阅读”中的每页帖子设置为9我使用了上面链接中答案提供的部分代码(请参见下面的代码片段)。这被放在我的函数中。php代码添加到函数中。php:
function tax_and_offset_homepage( $query ) {
if ($query->is_home() && $query->is_main_query() && !is_admin()) {
$query->set( \'post_type\', \'post\' );
$query->set( \'post_status\', \'publish\' );
$query->set(\'ignore_sticky_posts\', true);
$ppp = get_option(\'posts_per_page\'); // 9
$offset = 1;
if (!$query->is_paged()) {
$query->set(\'posts_per_page\',$offset + $ppp);
} else {
$offset = $offset + ( ($query->query_vars[\'paged\']-1) * $ppp );
$query->set(\'posts_per_page\',$ppp);
$query->set(\'offset\',$offset);
}
}
}
add_action(\'pre_get_posts\',\'tax_and_offset_homepage\');
function homepage_offset_pagination( $found_posts, $query ) {
$offset = 1;
if( $query->is_home() && $query->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( \'found_posts\', \'homepage_offset_pagination\', 10, 2 );
添加了所有这些内容后,第一页显示了9篇文章(后面的所有页面也是如此)。第十篇帖子没有出现在第一页上,但第二页的顶部也没有出现(第十篇最近的帖子从未出现),所以我想我可能很接近了。
有人能告诉我我做错了什么吗?
编辑:似乎不尊重posts\\u per\\u页面。我在上面代码中的第一个if语句中打印了$查询,返回的结果如下:
WP_Query Object
(
[query] => Array
(
[page] =>
[pagename] => news
)
[query_vars] => Array
(
[page] =>
[pagename] => news
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[post_type] => post
[post_status] => publish
[ignore_sticky_posts] => 1
[posts_per_page] => 10
)
[tax_query] =>
[meta_query] =>
[date_query] =>
[queried_object] => WP_Post Object
(
[ID] => 1205
[post_author] => 1
[post_date] => 2017-10-13 14:51:41
[post_date_gmt] => 2017-10-13 14:51:41
[post_content] =>
[post_title] => News
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => news
[to_ping] =>
[pinged] =>
[post_modified] => 2017-10-13 14:51:41
[post_modified_gmt] => 2017-10-13 14:51:41
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://64.37.102.163/~mc5movie/?page_id=1205
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[queried_object_id] => 1205
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] => 1
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => aea7eb467f9dbaa315c6d24870ff42f2
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
感谢您的帮助!我已经找了几天了,还没有找到答案。