You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义模板中WooCommerce分页功能失效问题求助

WooCommerce自定义模板分页异常修复方案

环境信息

  • WordPress版本:6.2.2
  • WooCommerce版本:7.8.2
  • 固定链接设置:/%postname%/
  • 产品固定链接设置:/produkt/
  • 使用主题:TwentyTwentyOne

问题描述

自定义模板custom-template.php展示WooCommerce产品时,仅能正常显示前22个产品,分页功能失效:点击分页按钮始终返回前22个产品,点击第2页时URL变为www.website.com/page/2/。

用户提供的原始代码

<div id="content">
<?php
$paged = max(1, get_query_var('paged')); // Ensure the current page is at least 1

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 22,
    'paged' => $paged
);

$products = new WP_Query($args);

if ($products->have_posts()) {
    echo '<div class="table"><div class="table-thead"><div class="table-tr"></div></div><div class="table-tbody">';
    while ($products->have_posts()) {
        $products->the_post();
        global $product;

        // Get the product data
        $product_name = $product->get_name();
        $product_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
        $product_permalink = get_permalink();

        // Output the product details
        echo '<div class="table-tr">';
        echo '<div class="table-td entryImage"><img src="' . $product_image[0] . '" alt="' . $product_name . '"></div>';
        echo '<div class="table-td entryName"><h2 class="c1"><a class="c1" href="' . $product_permalink . '">' . $product_name . '</a></h2></div>';

        // Additional product attributes
        $attributes = $product->get_attributes();
        if ($attributes && !empty($attributes)) { // Add a check for $attributes
            $counter = 1;
            foreach ($attributes as $attribute) {
                $attribute_label = wc_attribute_label($attribute->get_name());
                $attribute_value = array();

                // Get attribute options as labels
                $options = $attribute->get_terms();
                foreach ($options as $option) {
                    $attribute_value[] = $option->name;
                }

                $css_class = 'entry' . $counter;

                echo '<div class="table-td ' . $css_class . '"><span class="attribute-label c1">' . $attribute_label . ':</span> <span class="attribute-value">' . implode(', ', $attribute_value) . '</span></div>';

                $counter++;
            }
        }

        echo '</div>';
    }
    echo '</div></div>';

    if ($products->max_num_pages > 1) {
        $pagination_args = array(
            'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
            'format' => '?paged=%#%',
            'current' => $paged,
            'total' => $products->max_num_pages,
            'prev_text' => '<< Zuruck',
            'next_text' => 'Weiter >>',
            'mid_size' => 5, // Number of links to display on each side of the current page
        );

        echo '<div class="pagination paginationtop">';
        echo paginate_links($pagination_args);
        echo '</div>';
    }
} else {
    echo 'No products found.';
}

wp_reset_postdata();
?>
</div>

修复步骤及原理

1. 修正页码获取逻辑

自定义页面模板中,WordPress会使用page查询变量而非paged,原代码仅获取paged变量导致页码始终为1。替换原有页码获取代码:

// 原代码
// $paged = max(1, get_query_var('paged'));

// 修正后
$paged = max(1, get_query_var('paged') ?: get_query_var('page'));

2. 调整分页链接参数

原分页链接的format参数使用查询字符串格式,与当前固定链接的路径式结构不匹配,导致分页参数无法被正确识别。修改分页参数:

// 原分页参数
// $pagination_args = array(
//     'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
//     'format' => '?paged=%#%',
//     ...
// );

// 修正后
$pagination_args = array(
    'base' => trailingslashit(get_permalink()) . 'page/%#%/',
    'format' => 'page/%#%/',
    'current' => $paged,
    'total' => $products->max_num_pages,
    'prev_text' => '<< Zuruck',
    'next_text' => 'Weiter >>',
    'mid_size' => 5,
);

trailingslashit(get_permalink())确保当前页面链接末尾带斜杠,拼接page/%#%/生成符合固定链接规则的分页路径。

3. 完整修复后代码

将上述修改应用后,核心代码片段如下(产品循环部分保持不变):

<div id="content">
<?php
$paged = max(1, get_query_var('paged') ?: get_query_var('page'));

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 22,
    'paged' => $paged
);

$products = new WP_Query($args);

if ($products->have_posts()) {
    // 产品循环输出代码保持不变...

    if ($products->max_num_pages > 1) {
        $pagination_args = array(
            'base' => trailingslashit(get_permalink()) . 'page/%#%/',
            'format' => 'page/%#%/',
            'current' => $paged,
            'total' => $products->max_num_pages,
            'prev_text' => '<< Zuruck',
            'next_text' => 'Weiter >>',
            'mid_size' => 5,
        );

        echo '<div class="pagination paginationtop">';
        echo paginate_links($pagination_args);
        echo '</div>';
    }
} else {
    echo 'No products found.';
}

wp_reset_postdata();
?>
</div>

验证

修改后刷新页面,点击分页按钮,URL将保持www.website.com/page/2/结构,页面会正确加载对应页码的产品列表。

内容的提问来源于stack exchange,提问作者stst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:35:01