function get_parent_terms($term) { if ($term->parent > 0) { $term = get_term_by("id", $term->parent, "product_cat"); if ($term->parent > 0) { get_parent_terms($term); } else { return $term; } } else { return $term; } }
function all_cat_classes($post) { $catz = array(); $terms = get_the_terms($post->id, 'product_cat'); if (is_array($terms)) { // foreach product_cat get main top product_cat foreach ($terms as $cat) { $catz = get_parent_terms($cat); //$cats .= (strpos($cats, $cat->slug) === false ? $cat->slug." " : ""); } } return $catz; }