【问题标题】:How to get css ID from product category name in Wordpress如何从 Wordpress 中的产品类别名称中获取 css ID
【发布时间】:2015-03-19 17:55:33
【问题描述】:

我使用 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 中做到这一点。

【问题讨论】:

  • 答案在 get_the_term_list() 函数中

标签: php css wordpress woocommerce


【解决方案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; ?>

【讨论】:

  • 你能检查我的新答案吗@DannyVanHolten
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2017-05-26
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多