Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Last active December 5, 2024 15:16
Show Gist options
  • Save emre-edu-tech/2bdff58386aa638f00017882352a2731 to your computer and use it in GitHub Desktop.
Save emre-edu-tech/2bdff58386aa638f00017882352a2731 to your computer and use it in GitHub Desktop.
Wordpress shortcode for getting the number of products or name for a specific category. Can be used to show number of products for the category.
<?php
// [leoshop_get_products_by_category_id category_id='87' property='cleaning']
add_shortcode('leoshop_get_products_by_category_id', 'leoshop_get_products_by_category_id');
function leoshop_get_products_by_category_id($attr) {
// get the attributes of shortcode
$args = shortcode_atts([
'category_id' => 87,
'property' => 'cat_name',
], $attr );
$term = get_term($args['category_id'], 'product_cat');
if($args['property'] == 'cat_name') {
return $term->name;
} else if($args['property'] == 'product_count') {
return $term->count;
} else {
return 'Nicht verfügbar';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment