How to get css ID from product category name in WordPress

I’m using woocommerce as a catalog. I want to give css id each product category from category id. Because i want to make “display:none” in some where.

My codes looking in Chrome console like this:

Read More
<div class="woo-category">
  <span>
    <a href="http://www.yazicimakina.com.tr/?product_cat=overlap" rel="tag">Overlap</a>
    ","
    <a href="http://www.yazicimakina.com.tr/?product_cat=tam" rel="tag">TAM</a>
    ","
    <a href="http://www.yazicimakina.com.tr/?product_cat=tumu" rel="tag">TÜMÜ</a>
  </span>
</div>

My PHP codes:

<div class="woo-category">
  <?php
    $postid = get_the_ID();
    $categories = get_the_term_list($postid, 'product_cat', '', ', ', '');
    ?>
    <span><?php print_r($categories); ?></span>
 </div>

I want to:

<div class="woo-category">
  <span>
    <a id="example1" href="http://www.yazicimakina.com.tr/?product_cat=overlap" rel="tag">Overlap</a>
    ","
    <a id="example2" href="http://www.yazicimakina.com.tr/?product_cat=tam" rel="tag">TAM</a>
    ","
    <a id="example3" href="http://www.yazicimakina.com.tr/?product_cat=tumu" rel="tag">TÜMÜ</a>
  </span>
</div>

I think way is: print to category id as a css id but I don’t know how can i do that in PHP.

Related posts

Leave a Reply

1 comment

  1. Best practice here is a custom loop. Try the following

    <?php
    $terms = get_the_terms( $post->ID, 'product_tag' );
    if ($terms && ! is_wp_error($terms)): ?>
        <?php foreach($terms as $term): ?>
            <a href="<?php echo get_term_link( $term->slug, 'product_tag'); ?>" rel="tag" class="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a>
        <?php endforeach; ?>
    <?php endif; ?>