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>
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>";
}
}
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>";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With