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

为WooCommerce单品页品牌产品输出添加分页功能

在WooCommerce单品页标签页上方添加同品牌分页产品列表

我来帮你搞定这个需求!咱们可以通过WooCommerce官方钩子+自定义WP查询来实现,直接上干货:

1. 核心功能代码实现

把下面的代码贴到你主题的functions.php里,或者自己整个小插件放进去都行:

add_action( 'woocommerce_before_product_tabs', 'display_brand_products_with_pagination', 10 );
function display_brand_products_with_pagination() {
    // 只在单品页生效,其他页面直接跳过
    if ( ! is_product() ) return;

    global $product;
    $current_product_id = $product->get_id();

    // 拿当前产品的品牌ID(WooCommerce Brands用的是`product_brand`这个分类)
    $brands = wp_get_post_terms( $current_product_id, 'product_brand', array( 'fields' => 'ids' ) );
    
    // 要是没品牌或者品牌为空,直接撤
    if ( empty( $brands ) ) return;

    // 处理分页参数,用自定义的`brand_paged`避免和全局分页参数冲突
    $paged = isset( $_GET['brand_paged'] ) ? absint( $_GET['brand_paged'] ) : 1;

    // 设置查询参数,每页3个产品,排除当前产品
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 3,
        'paged'          => $paged,
        'post__not_in'   => array( $current_product_id ),
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_brand',
                'field'    => 'term_id',
                'terms'    => $brands[0], // 默认取第一个品牌,多品牌的话可以调整这里
            ),
        ),
        'post_status'    => 'publish',
    );

    $brand_products_query = new WP_Query( $args );

    // 有产品才输出内容
    if ( $brand_products_query->have_posts() ) {
        $brand_name = get_term( $brands[0], 'product_brand' )->name;
        ?>
        <section class="brand-related-products">
            <h2><?php echo esc_html( $brand_name ); ?> 系列产品</h2>
            <div class="brand-products-grid">
                <?php while ( $brand_products_query->have_posts() ) : $brand_products_query->the_post(); ?>
                    <?php wc_get_template_part( 'content', 'product' ); // 用主题自带的产品卡片模板,风格统一 ?>
                <?php endwhile; ?>
            </div>
            <!-- 分页导航 -->
            <div class="brand-products-pagination">
                <?php
                $pagination_args = array(
                    'base'      => add_query_arg( 'brand_paged', '%#%' ),
                    'format'    => '',
                    'current'   => max( 1, $paged ),
                    'total'     => $brand_products_query->max_num_pages,
                    'prev_text' => '&laquo; 上一页',
                    'next_text' => '下一页 &raquo;',
                    'type'      => 'list',
                );
                echo paginate_links( $pagination_args );
                ?>
            </div>
        </section>
        <?php
        // 重置查询,避免影响后面的内容
        wp_reset_postdata();
    }
}

2. 样式优化(可选)

如果觉得默认布局不好看,加段CSS调整下网格和分页的样式,贴到主题的自定义CSS里就行:

.brand-related-products {
    margin: 30px 0;
    padding: 20px;
    border: 1px solid #eee;
    border-radius: 4px;
}

.brand-products-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.brand-products-pagination ul {
    list-style: none;
    padding: 0;
    display: flex;
    gap: 10px;
    justify-content: center;
}

.brand-products-pagination a,
.brand-products-pagination span {
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    text-decoration: none;
    color: #333;
}

.brand-products-pagination .current {
    background-color: #2d7dd2;
    color: white;
    border-color: #2d7dd2;
}

几个关键细节要注意

  • 钩子选择:用woocommerce_before_product_tabs绝对靠谱,这个钩子就是专门用来在单品页标签页上方输出内容的,官方维护的兼容性拉满。
  • 分页参数:为啥用brand_paged而不是默认的paged?因为单品页是单页面,用默认参数容易和全局分页逻辑冲突,自定义参数就没这问题了。
  • 多品牌处理:代码默认取产品的第一个品牌,要是你的产品会挂多个品牌,你可以把$brands[0]改成循环遍历所有品牌,或者加个判断选你要优先显示的那个。
  • 产品模板:用wc_get_template_part调用主题自带的产品卡片,这样出来的样式和你网站其他产品列表完全一致,不用额外调样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:36:16