Hugh's Blog

WordPress 单篇文章添加分页

问题:在单篇文章模板调用分类查询会 301 跳转回到原页面。

例如:自定义文章类型 author,模板为 single-author.php,分页链接 /author/example/page/2 会 301 重定向到 /author/example

原因:WordPress 对于分页查询参数会有判断,例如 is_single,满足的话会移除参数。

解决方法:remove_action('template_redirect', 'redirect_canonical')

function post_type_author_template_redirect()
{
    if (is_singular('author')) {
        global $wp_query;
        $page = (int)$wp_query->get('page');
        if ($page > 1) {
            $query->set('page', 1);
            $query->set('paged', $page);
        }
        remove_action('template_redirect', 'redirect_canonical');
    }
}
add_action('template_redirect', 'post_type_author_template_redirect', 0);

参考

How to add pagination to a single post

WP_Query Pagination on single-custom.php