8

Through a series of specific requirements, I find myself needing to link to a custom taxonomy category using its term id...

I've got this - which displays a link to all taxonomies - I wish to change it so it only displays a link to the taxonomy with the term id dynamically pulled from a custom field I'm using.

$taxonomy = 'event-categories';
$terms = get_terms($taxonomy);
if ($terms) {
foreach($terms as $term) {
echo '<li><a href="http:/mysite.com/events/categories/project-events/' . $term->slug . '">' . $term->name .'</a></li>';
 }
};

essentiall I need "link_to_taxonomy_category(x)" where x = term_id

Thanks

JorgeLuisBorges
  • 528
  • 3
  • 8
  • 21

2 Answers2

15

The function you are looking for is get_term_link. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.

As a side note hard coding the link as you have in the example above is fragile -- always keep your code as portable as possible. If the site is moved to a different domain, that link will break. WordPress has several functions that generate links dynamically based on the current installation environment. get_term_link is one example.

From the Codex:

$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
    echo '<li><a href="'.get_term_link($term->slug, 'species').'">'.$term->name.'</a></li>';
}
echo '</ul>';
gradyetc
  • 761
  • 4
  • 8
  • Hi, thanks - wI have actually tried that, but couldb't get it to work, i'll have another go now. Hard code is just in there for clarity in the question... – JorgeLuisBorges Apr 11 '12 at 13:06
  • yeah - can't really figue out how to use this - tried `$terms = get_terms('event-categories'); echo '';` and got the following error "Catchable fatal error: Object of class WP_Error could not be converted to string in..." – JorgeLuisBorges Apr 11 '12 at 13:08
  • 1
    The second argument for `get_term_link` should be the name of the taxonomy ('event-categories' in this case). It'll return an object of class WP_Error if the term or taxonomy doesn't exist. You can catch the value of the error message by saving it to a variable (i.e. $term_link) and examining it before echo'ing: `if ( is_wp_error($term_link) ) echo $term_link->get_error_message();` – gradyetc Apr 11 '12 at 13:36
  • I think I might be out of my depth here - not really sure how to use thid - sorry! – JorgeLuisBorges Apr 11 '12 at 13:48
  • 1
    `$terms = get_terms('event-categories'); echo '';` – gradyetc Apr 11 '12 at 14:25
  • thats great - 'event-categories' is my taxonomy - and this lists all the categories in that taxonomy, I'm trying to list just one of those categories - identitfied by its term id – JorgeLuisBorges Apr 11 '12 at 15:36
  • 1
    Then don't loop over the terms. If you have a term ID, all you need is: `get_term_link( $term_id, 'event-categories' );`. That will return the URL for the specific term you are looking for in the 'event-categories' taxonomy. – gradyetc Apr 11 '12 at 21:33
  • Sorry this just isn't working for me -must be a bug in my taxonomies as I'm fairly certain this is the right method - I've ended up working around in a plugin specific way - one that doesn;t really answer the question - thanks for your help though... – JorgeLuisBorges Apr 12 '12 at 10:20
6

If you have single term_id e.g: 10, custom taxonomy series then you can use the following code to get the taxonomy term link.

note : change 10 to your variable for term_id and 'series' to your taxonomy.

$term = get_term( 10, 'series' );
$term_link = get_term_link( $term );
echo '<a href="' . $term_link . '">View All</a>';
Aamer Shahzad
  • 2,617
  • 1
  • 27
  • 25