2015-01-21 171 views
1

我正在使用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

回答

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

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

0

我想我告訴不對勁......我想打印「類別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

相關問題