Archived topic
Woocommerce / Display Category Name above Product Titel
7 replies · Started by Kim on November 5, 2019
Hi there, it would be nice to have an option to display the respective category name above each product on the shop archive pages and/or the single product pages. Some themes, like Flatsome for example, provide this option.
Best regards!
Hi there,
thats interesting. Something we can consider.
You can do this with a Hook Element.
This is the PHP required to display the first category:
<?php
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) :
if ( ! empty( $terms ) ) {
echo $terms[0]->name;
}?>
<?php endif;?>
The Hook would be the:
woocommerce_after_shop_loop_item_title
I think the review stars are hooked in at 5 so the priority would need to be 4 or lower.
Hi David, works perfectly :) But how can I style this?
Change this:
echo $terms[0]->name;
to:
echo '<span class="woo-custom-category">' . $terms[0]->name . '</span>';
You can then style it with the .woo-custom-category CSS class.
Very nice! Thank you :)
Happy to be of help
Hola David,
He agregado este hook, sin embargo no muestra el nombre de la categoría.
Este hook es para mostrar la categoría en la tarjeta del producto que aparece en las páginas de categorías y tienda?
Como esta tarjeta de producto que encontré: https://mega.nz/file/2scyWDjC#LTOuQu63JXwYLYVpdHr6XviGOkqZfQtF3vazii_nk_Y
Hi Pedro,
As this is already solved for the topic starter, can you open up a new topic? So you can use the private information text field incase it is needed.
woocommerce_after_shop_loop_item_title is for the shop product items.