2017-08-03 50 views
2

我創建了一個名爲'tema'的自定義分類法,分類法有三個術語。我想顯示所有與當前帖子相關的術語鏈接。目前我只能得到我的代碼顯示的職位分類術語之一...如何在自定義帖子類型上顯示多於1個自定義分類術語或鏈接?

我想通過我的自定義content.php文件(「content-home.php」)顯示的術語鏈接顯示,用於顯示我的主頁上的自定義帖子摘錄。

目前我有這段代碼放在我的自定義content.php文件,它實際上工作正常,但我只能得到它顯示一個詞:

<?php 

    $terms = get_the_terms($post->ID, 'tema'); 

    foreach($terms as $term) { 
      echo '<a href="' . get_term_link($term) . '"><span>' . $term->name . '</span></a>'; 
    } 
?> 

誰能請告訴我我如何去顯示所有帖子分類術語鏈接?

+0

你* print_r($ terms); *你有多少媽媽? –

回答

0

在WordPress的食品,你可以找到:

對於get_the_terms: 「檢索附加到崗位分類法的條款。」 http://codex.wordpress.org/Function_Reference/get_the_terms

對於get_terms: 「檢索分類法或分類列表中的術語」。 http://codex.wordpress.org/Function_Reference/get_terms

所以,get_the_terms()將獲得附加到相應條款(​​例如分類),而get_terms()將檢索分類法的條款(如類別在類別分類)。例如,get_terms('category')將返回您添加到您的WordPress網站的所有類別。

你應該使用這樣的事情:

<?php     
    $terms= get_terms(array('taxonomy'=>'tema')); 
    foreach($terms as $term){ 
     echo '<a href="' . get_term_link($term) . '"><span>' . $term->name . '</span></a>'; 
    } 
?> 
+0

真棒(y)..超級的東西。 –

+0

謝謝,但它在我的所有帖子上吐出了所有三個術語。我需要它僅顯示適用於特定帖子的條款。你能幫我嗎? :-) – MariaThiim

+0

然後你的代碼應該工作。在<?php下使用你的代碼if(have_posts()):while(have_posts()):the_post(); /*您的代碼在這裏* /結束;萬一; ?> –

0

嘗試下面鉤子函數來獲取特定帖子的ID的分類列表,

//Returns All Term Items for "my_taxonomy" 
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all")); 
print_r($term_list); 

* my_taxonomy - 更換您的分類

https://codex.wordpress.org/Function_Reference/wp_get_post_terms

+0

謝謝GNANA,但它然後打印關於分類術語的所有信息,這不是我想要的...它仍然不顯示在特定帖子上使用的術語... – MariaThiim

+0

你需要使用分類標準嗎? – GNANA

+0

如果是的話,試試這個https://wordpress.stackexchange.com/questions/66219/list-all-posts-in-custom-post-type-by-taxonomy – GNANA

相關問題