在 Woocommerce 3.3 中为产品简码使用自定义分类法
Using a custom taxonomy for products shortcode in Woocommerce 3.3
我目前正在为 Woocommerce 网站开发主页,在此页面上的目标是让 3 行显示来自不同品牌的产品。例子;第 1 行显示 Apple 产品,第 2 行显示三星产品,第 3 行显示 HTC 产品。
我使用插件 CPT UI 来创建自定义分类法 'Brand'。现在我想使用上面的示例仅显示特定品牌下列出的产品。
查看 Woocommerce 简码,我看到了这个:
[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="AND"]
有没有办法为这种案例品牌做这样的自定义分类?即:
[products limit="8" columns="4" brand="apple" cat_operator="AND"]
非常感谢任何正确方向的帮助或推动!
可以将 Woocommerce [products]
简码扩展到使用某种技巧来处理任何自定义分类法,例如 "brand"
。
代码:
add_filter( 'woocommerce_shortcode_products_query', 'extend_products_shortcode_to_brand', 10, 3 );
function extend_products_shortcode_to_brand( $query_args, $atts, $loop_name ){
if ( ! empty($atts['class']) && strpos($atts['class'], 'brand') !== false ) {
global $wpdb;
$terms = array_map( 'sanitize_title', explode( ',', $atts['class'] ) );
array_shift( $terms );
$terms = implode(',', $terms);
$terms = str_replace(",", "','", $terms);
$ids = $wpdb->get_col( "
SELECT DISTINCT tr.object_id
FROM {$wpdb->prefix}term_relationships as tr
INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
WHERE tt.taxonomy LIKE 'brand' AND t.slug IN ('$terms')
" );
if ( ! empty( $ids ) ) {
if ( 1 === count( $ids ) ) {
$query_args['p'] = $ids[0];
} else {
$query_args['post__in'] = $ids;
}
}
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法
我们将在此处使用 class
简码参数:
1) 一个品牌 - 显示来自 "Apple" 品牌的产品:
[products limit="8" columns="4" class="brand,Apple"]
2) 多个品牌 - 显示来自 "Apple" 和 "Samsung" 品牌的产品:
[products limit="8" columns="4" class="brand,Apple,Samsung"]
So the class "brand"
is mandatory and needs to be the first one. Each term is coma separated.
我目前正在为 Woocommerce 网站开发主页,在此页面上的目标是让 3 行显示来自不同品牌的产品。例子;第 1 行显示 Apple 产品,第 2 行显示三星产品,第 3 行显示 HTC 产品。
我使用插件 CPT UI 来创建自定义分类法 'Brand'。现在我想使用上面的示例仅显示特定品牌下列出的产品。
查看 Woocommerce 简码,我看到了这个:
[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="AND"]
有没有办法为这种案例品牌做这样的自定义分类?即:
[products limit="8" columns="4" brand="apple" cat_operator="AND"]
非常感谢任何正确方向的帮助或推动!
可以将 Woocommerce [products]
简码扩展到使用某种技巧来处理任何自定义分类法,例如 "brand"
。
代码:
add_filter( 'woocommerce_shortcode_products_query', 'extend_products_shortcode_to_brand', 10, 3 );
function extend_products_shortcode_to_brand( $query_args, $atts, $loop_name ){
if ( ! empty($atts['class']) && strpos($atts['class'], 'brand') !== false ) {
global $wpdb;
$terms = array_map( 'sanitize_title', explode( ',', $atts['class'] ) );
array_shift( $terms );
$terms = implode(',', $terms);
$terms = str_replace(",", "','", $terms);
$ids = $wpdb->get_col( "
SELECT DISTINCT tr.object_id
FROM {$wpdb->prefix}term_relationships as tr
INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
WHERE tt.taxonomy LIKE 'brand' AND t.slug IN ('$terms')
" );
if ( ! empty( $ids ) ) {
if ( 1 === count( $ids ) ) {
$query_args['p'] = $ids[0];
} else {
$query_args['post__in'] = $ids;
}
}
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法
我们将在此处使用 class
简码参数:
1) 一个品牌 - 显示来自 "Apple" 品牌的产品:
[products limit="8" columns="4" class="brand,Apple"]
2) 多个品牌 - 显示来自 "Apple" 和 "Samsung" 品牌的产品:
[products limit="8" columns="4" class="brand,Apple,Samsung"]
So the class
"brand"
is mandatory and needs to be the first one. Each term is coma separated.