Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output permalink for term on WordPress posts in loop?

I have a series of posts within a custom post type which are all have a term within the taxonomy "collection." Each post is associated with no more than one term within the "collection" taxonomy. I want to create a link under each post that says something like "More in this Collection," how can I dynamically create a link to the term that it belongs in?

When I use the following snippet, it shows a list of terms as links. I just need the permalink so I can create that custom link, and not the name of the term associated with it.

<?php echo get_the_term_list( $post->ID, 'collection', '', ', ', '' ); ?>

What I'm trying to accomplish is a dynamic way to write something like this:

<a href="TERM_PERMALINK">More in this Collection</a>
like image 751
nurain Avatar asked Jan 24 '26 13:01

nurain


2 Answers

You can use get_term_link() function also.

http://codex.wordpress.org/Function_Reference/get_term_link

Here is a small example:

$terms = get_the_terms($post->ID, 'my_taxonomy');

if (! empty($terms)) {
  foreach ($terms as $term) {
    $url = get_term_link($term->slug, 'my_taxonomy');
    print "<a href='{$url}'>{$term->name}</a>";
  }
}
like image 135
Andrey Rudenko Avatar answered Jan 27 '26 02:01

Andrey Rudenko


Here's an example using get_term_link() and actually outputting the link as you described.

$collections = get_the_terms($post->ID, 'collection');

foreach ($collections as $collection){
    echo "<a href='".get_term_link($collection->slug, 'collection')."'>".$collection->name."</a>";
}
like image 43
MarcGuay Avatar answered Jan 27 '26 03:01

MarcGuay