首页 文章

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

提问于
浏览
2

我创建了一个名为“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

    在WordPress Codex中,您可以找到:

    对于get_the_terms:"Retrieve the terms of the taxonomy that are attached to the post." http://codex.wordpress.org/Function_Reference/get_the_terms

    对于get_terms:"Retrieve the terms in a taxonomy or list of taxonomies." 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

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

相关问题