如何使用 WooCommerce 更改每行的产品类别数

我使用 Woocommerce 设置在初始商店页面上显示类别缩略图,然后在其中显示产品及其缩略图.

I'm using Woocommerce settings to show categories thumbnail on the initial shop page and then products and their thumbnails within them.

我希望初始类别页面每行显示 3 个缩略图,产品页面每行显示 5 个类别.

I want to have that initial category page to display 3 thumbnails per row and the products page to show 5 categories per row.

每行显示 5 个我使用过的产品:

To display 5 products per row I've used:

add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns() {
    return 5;
    }
}

这会更改类别页面和商店页面上每行的缩略图.

This changes the thumbnails per row on the category page AND on the shop page too.

有谁知道如何将类别页面更改为每行 3 个缩略图并在商店页面上保持每行 5 个产品?

Does anyone know how I can change the categories page to 3 thumbnails per row and maintain 5 products per row on shop page?

推荐答案

使用 WooCommerce 条件标签,将帮助您实现这一目标.我已经更改了您的代码:

Using WooCommerce conditionals tags, will help you to achieve that. I have changed your code:

add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns() {
        if ( is_product_category() ) {
            return 3;
        } else { // for other archive pages and shop page
            return 5;
        }
    }
}

此代码位于活动子主题或主题的 function.php 文件中

建议:有时,需要更改一些 css 规则,以获得每行的正确显示.

Advice: Sometimes, is necessary to change some css rules, to get the correct display per row.

WooCommerce 条件标签用法:

要定位商店页面档案:

if ( is_shop() ) {
    // return the number of items by row
}

要定位产品标签档案:

if ( is_product_tag() ) {
    // return the number of items by row
}

定位所有产品档案,除了产品类别档案(添加开头):

if ( !is_product_category() ) {
    // return the number of items by row
}

您还可以定义一些特定的类别或标签(请参阅相关文档).

And you can also define some particular categories or tags (see the documentation for that).

<小时>

参考文献:


References:

  • WooCommerce - 更改每行的产品数量
  • WooCommerce - 条件标签

相关文章