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

How to change the number of product categories per row using WooCommerce

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

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

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

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

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

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

使用WooCommerce conditionals tags,将帮助您实现这一目标。我已经更改了您的代码:

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 规则,以获得每行的正确显示。

WooCommerce conditionals tags usage:

To target shop page archive:

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

To target product tag archives:

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

To target all product archives except product category archives (adding ! at the beginning):

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).


参考文献:

在我的 storefront-child 主题中。接受的答案对我不起作用。我必须将 woocommerce/templates/loop/loop-star.php 复制到我的子主题子文件夹 /woocommerce/loop/ 并像这样修改它:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// FMP20181126 - Categories are SubCategories set # of columns.
$display_type = woocommerce_get_loop_display_mode();
if ( 'subcategories' === $display_type || 'both' === $display_type ) {
        wc_set_loop_prop( 'columns', 3 );
}   

?>

<ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">