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

时间:2017-08-03 08:35:27

标签: php wordpress custom-taxonomy taxonomy-terms

我创建了一个名为“tema”的自定义分类法。分类学有三个术语。我想显示与当前帖子相关的所有术语链接。目前我只能让我的代码显示一个帖子分类术语......

我希望通过我的自定义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>';
    }
?>

任何人都可以告诉我如何让它显示所有帖子分类术语链接?

2 个答案:

答案 0 :(得分:0)

在WordPress Codex中,您可以找到:

对于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>';
  }
?>

答案 1 :(得分: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