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

时间:2015-01-21 09:15:08

标签: php css wordpress woocommerce

我正在使用woocommerce作为目录。我想从类别id给每个产品类别的css id。因为我想在某些地方制作“display:none”。

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

我的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>

我想:

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

我认为方法是:将类别ID打印为css id,但我不知道如何在PHP中执行此操作。

1 个答案:

答案 0 :(得分:1)

这里的最佳做法是自定义循环。请尝试以下

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