Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Last active August 19, 2021 15:08
Show Gist options
  • Save jmarreros/47b97dd59627f219cca70cebcb4e101a to your computer and use it in GitHub Desktop.
Save jmarreros/47b97dd59627f219cca70cebcb4e101a to your computer and use it in GitHub Desktop.
Muestra las categorías de un producto en la lista de productos de WooCommerce
<?php // No copiar esta línea
add_action( 'woocommerce_after_shop_loop_item_title', 'dcms_categories_item_product', 10, 0 );
function dcms_categories_item_product() {
if ( ! is_shop() ) return;
global $product;
$product_data = $product->get_data();
$category_ids = $product_data['category_ids'];
if ( ! count($category_ids) ) return;
print("<section class='item-categories'>");
foreach ($category_ids as $category_id){
$term_object = get_term_by('id', $category_id, 'product_cat');
$category_name = $term_object->name;
$category_link = get_category_link($category_id);
printf("<a href='%s' title='%s'>%s</a>",$category_link, $category_name, $category_name);
}
print("</section>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment