skip to Main Content

There is a cycle that outputs products

<?php
$args = array('post_type' =>
'product');
$loop = new WP_Query($args);
foreach ($loop->posts as $have_post) : ?>
  <div class="swiper-slide">
    <a href="
                  <?php
                  $permalink = get_post_permalink($have_post->ID);
                  echo $permalink;
                  ?>
                  " class="goods__item">
      <div class="goods__text">
        <?php
        echo $have_post->name; ?>
      </div>
      <div class="goods__img">
        <?php
        echo get_the_post_thumbnail($have_post->ID, 'full', array('alt' => $have_post->post_title, 'data-product_id' => $have_post->ID)); ?>
      </div>
      <div class="goods__name"><?php echo $have_post->post_title; ?></div>
      <div class="goods__price">
        <?php
        $price = get_post_meta($have_post->ID, '_price', true);
        echo $price; ?> руб.
      </div>
    </a>
  </div>
<?php endforeach; ?>

Need to show the category of current product here. I cant get the category. Please help

<div class="goods__text"><?php echo $have_post->name; ?></div>

2

Answers


  1. You could use get_the_terms to get the correct terms. I believe it’s product_cat the correct taxonomy.

    $terms = get_the_terms( $have_post->ID, 'product_cat' );
    foreach ($terms as $term) {
        $product_cat_id = $term->term_id;
        $product_cat_name = get_cat_name($product_cat_id); 
        break;
    }
    
    Login or Signup to reply.
  2. Try get_the_category( $post->ID );

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search