问题
I am using Woocommerce 3.3.3 and trying to change the number of columns for a specific product category and not the other product categories. I have tried several code options, but they all apply the change in columns to all categories. Here is one of thing I've tried.
/* change columns to 1 for newsletter issues prod cat*/
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
if ( is_product_category('newsletter-issues')) {
return 1;
} else { // for other archive pages and shop page
return 4;
}
}
}
回答1:
Using the bellow function you can set product list column and number of product per category page.
/* Set product columns for specific category */
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
if ( is_product_category('a')) {
return 3;
} else if ( is_product_category('c')) {
return 2;
}else{
return 4;
}
}
}
/* Set product per page for specific category */
add_action( 'woocommerce_product_query', 'cdx_woocommerce_products_per_page', 1, 50 );
function cdx_woocommerce_products_per_page( $query ) {
if( $query->is_main_query() && isset($query->query['product_cat'])){
switch($query->query['product_cat']){
case 'a':
$query->set( 'posts_per_page', '3' );
break;
case 'c':
$query->set( 'posts_per_page', '2' );
break;
default:
$query->set( 'posts_per_page', '4' );
}
}
}
- Here a and c is product category slug. You can change it as per your required category slug.
- You need to add this code inside your theme function.php
来源:https://stackoverflow.com/questions/49456778/change-number-of-columns-products-per-row-for-specific-woocommerce-product-cate