probablybest
probablybest

Reputation: 1445

Wordpress - Single taxonomy term to link to category of term

Im currently displaying the custom taxonomy term for my post on a single-resources.php page. However I need it to link to the taxonomy category page and not the link of the page.

This is what I currently have:

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

I was previously doing this which does work however its displaying every taxonomy term rather than the one specific to the post, so it doesn't work :(

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

Does anyone have any idea on someway to combine the two?

Upvotes: 0

Views: 2811

Answers (1)

probablybest
probablybest

Reputation: 1445

For anyone else having an issue with this I managed to achieve what I was after with the following code:

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

You need to use get_the_terms instead of get_terms. As mentioned in the comments, dont use wp_get_post_terms as this causes unnecessary calls to the database

Upvotes: 1

Related Questions