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

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

问题描述:

我正在使用woocommerce作为目录。我想给类别ID的每个产品类别的CSS ID。因为我想在某些地方制作“display:none”。如何在Wordpress中从产品类别名称获取css ID

我看的代码在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中做到这一点。

+0

答案是get_the_term_list某处()函数 – 2015-01-21 09:19:14

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

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

你能否检查我的新答案请@DannyVanHolten – 2015-01-21 09:45:57

我想我告诉不对劲......我想打印“类别ID”为“CSS ID” ......我把你的代码,并将其变更为“文章标题”作为“分类名称”。现在它正在寻找

<div class="woo-category"> 
    <a href="http://www.yazicimakina.com.tr/?product_tag=4570-ss-2008" rel="tag" class="4570-ss-2008">4570 SS 2008</a> 
</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> 

我不想改变类别名称只是我想添加CSS ID每个类别...

@丹尼 - 范 - 霍尔特

+0

不要解释细节为答案,编辑你的问题。设置你的帖子的段落标题为ID – ilhnctn 2015-01-21 09:46:40

+0

请编辑你的问题,以便我可以正确回答。而不是回答你的问题。 – 2015-01-21 09:48:51

+0

我改变了..可以检查吗? – 2015-01-21 11:46:20