E
E
Eduard Valeev2019-07-03 23:11:15
WordPress
Eduard Valeev, 2019-07-03 23:11:15

woocommerce. How to display a product category map?

I want to display a map of product categories, approximately like here: https://obtorg.ru/sitemap/
Preferably with the number of products in the categories. How to implement it, tell me, please.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andrash_, 2019-07-04
@Adward

Fix it as needed. You can output the main loop variable to print_r if additional data is needed.

<?php $terms = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
    'pad_counts'=> true,
    'orderby' => 'name',
    'parent' => 0
  ) ); ?>  

  <?php if($terms) : ?>
  <?php foreach($terms as $term) : ?>

    <h2 class="subcategory-title"><a href="<?php echo get_term_link($term->term_id);?>"><?php echo $term->name;?></a></h2>
    <!--<div class="subcategory-descripton"><p><?php //if($term->description) : ?><?php //echo $term->description; ?><?php //else: ?>Описание термина таксономии<?php //endif; ?></p></div>-->

    <?php // Создаем массив терминов детей текущего термина текущей таксономии
    $tax = $term->taxonomy;
    $children_terms = get_terms( array(
      'taxonomy' => $tax,
      'hide_empty' => false,
      'parent' => $term->term_id
    ) ); ?>

    <?php if($children_terms): // если есть дочерние категории ?>
      <ul><?php foreach ($children_terms as $children_term) : ?>
      <?php $link = get_term_link($children_term); ?>

        <li style="margin-left: 10px;"><a href="<?php echo $link ?>";><?php echo $children_term->name ?></a></li>
        <!--<p class="count"><?php //echo $children_term->count ?></p>-->

      <?php endforeach; ?></ul>
    <?php endif; // конец условия - если есть дочерние термины таксономии ?>  

  <?php endforeach; ?>
  <?php endif; ?>

here is the output of the quantity. Should be placed under the title.
<p class="count"><?php if ($term->count > 0) : ?><?php echo $term->count; ?><?php else: ?>0<?php endif; ?></p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question