0

Need to add a link on all single pages for custom post type taxonomy terms which links back to the taxonomy term archive the single page belongs to.

Using this code but it adds links for all tax terms. I only need the one the single page is assigned to.

$terms = get_terms( array(
    'taxonomy' => 'portfolio',
) );

if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {

foreach ( $terms as $term ) {

echo '<a href="' . get_term_link( $term->slug, 'portfolio' ) . '">' . $term->name . '</a>';

    }
}

I added this code in the single-portfolio.php file

1

Seems like a good use for get_the_term_list() https://codex.wordpress.org/Function_Reference/get_the_term_list

  • I only want to get the term assigned to the single entry, not the entire list. Example: If in same term, link to the term archive. – Dev Sep 23 at 19:11
  • @Dev have you tried this? From the codex page it states "Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages." – czerspalace Sep 23 at 22:36
  • Yes i have but i only need one which the single post is related to. – Dev Sep 24 at 6:03
  • 1
    @Dev If you do $all_terms = wp_get_post_terms( get_the_ID(), 'portfolio' ); and then use $all_terms[0] does that solve the issue? – czerspalace Sep 25 at 0:10
  • 1
    Didn't work for me however i'll give you the update for effort. I may be wrong with the implementation at my end. – Dev Sep 25 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.