如何在自定义帖子类型上显示多于1个自定义分类术语或链接?

问题描述:

我创建了一个名为'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); *你有多少妈妈? –

在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

然后你的代码应该工作。在 –

尝试下面钩子函数来获取特定帖子的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