php – Display div if categroy has a specific parent

Question

I have a PHP snippet that displays a div on product pages based on their specific category ID.

How can I adapt this code to display the div based on whether the category is an ancestor of a specific parent?

I’d like to take a more blanket approach as opposed to singling out specific IDs if possible – here’s what I’ve been using so far (thank you in advance for any help):

function options_available() {
global $product;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    $product_cat_id = $term->term_id;
    break;
}
if ($product_cat_id == "54" || $product_cat_id == "84" || $product_cat_id == "2022" || $product_cat_id == "56" || $product_cat_id == "972" || $product_cat_id == "55" || $product_cat_id == "62" || $product_cat_id == "63" || $product_cat_id == "2024" || $product_cat_id == "2023" || $product_cat_id == "61" || $product_cat_id == "2210" || $product_cat_id == "2253" ) {
   echo '<div class="fkrow"> </div>';
}

0
David Cooper 1 month 2021-06-25T06:02:35-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse