I want to add the product's category between the title and the price of each product. As long as I edited the archive-product.php file with codes i found here, nothing seemed to work. I created a copy of the file on the active theme too.
CodePudding user response:
we can use 'woocommerce_after_shop_loop_item_title' action for this case
function product_category_in_shop_loop() {
global $product;
$product_id = $product->get_id();
$cat = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));
if( !empty($cat[0]) ){
echo '<p >'.$cat[0].'</p>';
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'product_category_in_shop_loop', 40 );
code goes to child theme function.php.