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

时间:2015-12-08 14:34:21

标签: php wordpress taxonomy term

我目前在单个resources.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>'; 
             }
         }?>

有人有什么想法将两者结合起来吗?

1 个答案:

答案 0 :(得分:1)

对于其他任何有此问题的人,我设法通过以下代码实现了我的目标:

  <?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,因为这会导致对数据库的不必要调用