Wordpress-链接到术语类别的单个分类术语


Wordpress - Single taxonomy term to link to category of term

我目前正在singleresources.php页面上显示我的文章的自定义分类术语。然而,我需要它链接到分类类别页面,而不是页面的链接。

这就是我目前拥有的:

<?php
    $term_list = wp_get_post_terms($post->ID, 'resourcecategory', array("fields" => "all"));
    foreach($term_list as $term_single) {
            echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term_single->name . '</span></a>';
    }
?>

我以前做过这样的工作,但它显示了每个分类术语,而不是特定于帖子的术语,所以它不起作用:(

<?php $terms = get_terms( 'resourcecategory' );
        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
             foreach ( $terms as $term ) {
               echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>'; 
             }
         }?>

有人想办法把两者结合起来吗?

对于其他对此有问题的人,我用以下代码实现了我想要的目标:

  <?php
    $terms = get_the_terms( $post->ID, 'resourcecategory');
    foreach($terms as $term) {
        echo '<a class="icon-hv-link" href="' . get_term_link($term) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>';
    }
  ?>

您需要使用get_the_terms而不是get_terms。如注释中所述,不要使用wp_get_post_terms,因为这会导致对数据库的不必要调用