如何在不使用 wp_query 的自定义 WordPress 页面上添加分页

问题描述 投票:0回答:1

我有一个包含电子商务项目的 WordPress 自定义页面。 (https://modeseeker.com/baskets-femme/

我想使用带有 ?page=2 等参数的分页。我知道 WordPress 有一个内置的分页系统,但我相信我不能使用它,因为我的后端不是 WordPress,所以我的项目不会使用 wp_query 显示。

问题是'page'和'p'是wordpress保留术语,所以当我尝试使用像https://modeseeker.com/baskets-femme/?page=2这样的网址时,它会被重定向到页面1 无参数我尝试在redirect_canonical()上使用add_filter禁用重定向,但随后收到错误404。

function page_number_found($redirect_url, $requested_url) {

if (isPageConcatenation($redirect_url, $requested_url)) {
    $redirect_url = $requested_url;
    }
    return $redirect_url;

add_filter('redirect_canonical', 'page_number_found', 10, 2);

我希望这会显示第 1 页,并且 url 中仍然有 ?page=2,但我收到错误 404。

所以我的问题是:有没有一种方法可以使用保留术语但不使用 Wordpress 查询来对我的页面进行分页?

提前致谢。

wordpress pagination
1个回答
0
投票

此代码验证 URL 中是否存在“page”参数,并相应地调整“paged”参数。确保将此代码合并到主题的functions.php 文件或自定义插件中。

function custom_pagination($query) {
    if (is_admin() || !$query->is_main_query()) {
        return;
    }

    if (isset($_GET['page']) && is_numeric($_GET['page'])) {
        $query->set('paged', $_GET['page']);
    }
}

add_action('pre_get_posts', 'custom_pagination');
© www.soinside.com 2019 - 2024. All rights reserved.