WordPress get_terms() function not display custom taxonomy categories for woocommerce

The following code I wrote is supposed to show all the product categories of a wordpress ecommerce website.

<?php $categories = get_terms( 
  array(
   'taxonomy' => 'product_cat',
   'hide_empty' => 'false',
   'numberposts' => -1)
  );
?>
<?php var_dump($categories); ?>
<?php foreach( $categories as $category ): ?>
 <h4 class="shop-category-name d-inline"><?php echo $category->name; ?></h4>
<?php endforeach; ?>

I’m using it inside a woocommerce hook that is responsible to render the contents before the main shop page, the woocommerce_before_main_content. I’m not able to get the categories, I will see only one category and the others are not listed. I’m not sure about, but maybe this can be something related to the fact that I’m using the function inside a woocommerce hook? I had a similar issue with the shop page featured image, I was not able to display it because of this motivation and I have modified the code to use the wc_get_page_ID('pag name').

Is there a fix ?

Leave a Comment