WooCommerce-更改每行产品类别的数量

T.D*_*Doe 3 php wordpress product categories woocommerce

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

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

要使用每行显示5种产品,请执行以下操作:

add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns() {
    return 5;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

先感谢您!

Loi*_*tec 6

使用WooCommerce条件标签可以帮助您实现这一目标。我更改了您的代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码将放在您活动的子主题或主题的function.php文件中

建议:有时,有必要更改某些CSS规则,以使每行显示正确。

WooCommerce条件标签的用法:

要定位商店页面存档:

if ( is_shop() ) {
    // return the number of items by row
}
Run Code Online (Sandbox Code Playgroud)

定位产品标签档案:

if ( is_product_tag() ) {
    // return the number of items by row
}
Run Code Online (Sandbox Code Playgroud)

定位除产品类别归档开头添加以外的所有产品归档 !

if ( !is_product_category() ) {
    // return the number of items by row
}
Run Code Online (Sandbox Code Playgroud)

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


参考文献: