admin管理员组文章数量:1321260
I would like to create a custom shortcode that count individually product-categories in Woocomerce but I haven't found any code on net. I need somethink like:
[counter product-category="example"]
Anyone can help me? I've no idea about php :(
<?php
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
?>
I would like to create a custom shortcode that count individually product-categories in Woocomerce but I haven't found any code on net. I need somethink like:
[counter product-category="example"]
Anyone can help me? I've no idea about php :(
<?php
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
?>
Share
Improve this question
asked Apr 15, 2018 at 19:09
PhineasDPhineasD
351 silver badge4 bronze badges
3
|
1 Answer
Reset to default 1Try this: (add it to the theme's main functions.php file)
add_shortcode( 'products-counter', 'products_counter' );
function products_counter( $atts ) {
$atts = shortcode_atts( [
'category' => '',
], $atts );
$taxonomy = 'product_cat';
if ( is_numeric( $atts['category'] ) ) {
$cat = get_term( $atts['category'], $taxonomy );
} else {
$cat = get_term_by( 'slug', $atts['category'], $taxonomy );
}
if ( $cat && ! is_wp_error( $cat ) ) {
return $cat->count;
}
return '';
}
You can use it like this:
- By specifying the product category ID:
[products-counter category="19"]
- By specifying the product category slug:
[products-counter category="hoodies"]
Example:
本文标签: woocommerce offtopicShortcode with product catgory counter
版权声明:本文标题:woocommerce offtopic - Shortcode with product catgory counter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742031713a2416564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
count
of the object. – mmm Commented Apr 15, 2018 at 19:38