Last active
December 5, 2024 15:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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