尝试以下操作:(将其添加到主题的main functions.php文件)
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 \'\';
}
您可以这样使用它:
通过指定产品类别ID:
[products-counter category="19"]
<通过指定产品类别
slug:
[products-counter category="hoodies"]
示例: