Hello, good afternoon.
I need to get the last child of a category within a product loop:
Categoria
-categoria filho
--categoria ultimo filho
This gives me always the Father, that is, 'Category':
<?php
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$single_cat = array_shift( $product_cats );
if( ! empty( $product_cats ) ) {
$single_cat = array_shift( $product_cats );
$top_term = $single_cat;
if( $top_term->parent > 0 ) {
while( $top_term->parent > 0 ) {
$top_term = get_term( $top_term->parent, 'product_cat' );
}
} else {
$attr = 'sem parent';
}
$attr = $top_term->slug;
} else {
$attr = 'sem categoria';
}
?>