简体   繁体   English

如何从Wordpress中的产品类别名称获取CSS ID

[英]How to get css ID from product category name in Wordpress

I'm using woocommerce as a catalog. 我正在使用woocommerce作为目录。 I want to give css id each product category from category id. 我想从类别ID中为每个产品类别提供CSS ID。 Because i want to make "display:none" in some where. 因为我想在某些地方制作“ display:none”。

My codes looking in Chrome console like this: 我在Chrome控制台中看到的代码如下:

<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: 我的PHP代码:

<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. 我认为方式是:打印到类别ID作为CSS ID,但我不知道如何在PHP中做到这一点。

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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM