get_term_link

Generate a permalink for a taxonomy term archive.

Description

(string|WP_Error) get_term_link( (object|int|string) $term, (string) $taxonomy = '' ); 

Returns (string|WP_Error)

HTML link to taxonomy term archive on success, WP_Error if term does not exist.

Parameters (2)

0. $term (object|int|string)
The term object, ID, or slug whose link will be retrieved.
1. $taxonomy — Optional. (string) => ''
Taxonomy. Default empty.

Usage

  1. if ( !function_exists( 'get_term_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The term object, ID, or slug whose link will be retrieved. 
  5. $term = null; 
  6.  
  7. // Optional. Taxonomy. Default empty. 
  8. $taxonomy = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_term_link($term, $taxonomy); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function get_term_link( $term, $taxonomy = '' ) { 
  2. global $wp_rewrite; 
  3.  
  4. if ( !is_object($term) ) { 
  5. if ( is_int( $term ) ) { 
  6. $term = get_term( $term, $taxonomy ); 
  7. } else { 
  8. $term = get_term_by( 'slug', $term, $taxonomy ); 
  9.  
  10. if ( !is_object($term) ) 
  11. $term = new WP_Error('invalid_term', __('Empty Term')); 
  12.  
  13. if ( is_wp_error( $term ) ) 
  14. return $term; 
  15.  
  16. $taxonomy = $term->taxonomy; 
  17.  
  18. $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); 
  19.  
  20. $slug = $term->slug; 
  21. $t = get_taxonomy($taxonomy); 
  22.  
  23. if ( empty($termlink) ) { 
  24. if ( 'category' == $taxonomy ) 
  25. $termlink = '?cat=' . $term->term_id; 
  26. elseif ( $t->query_var ) 
  27. $termlink = "?$t->query_var=$slug"; 
  28. else 
  29. $termlink = "?taxonomy=$taxonomy&term=$slug"; 
  30. $termlink = home_url($termlink); 
  31. } else { 
  32. if ( $t->rewrite['hierarchical'] ) { 
  33. $hierarchical_slugs = array(); 
  34. $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); 
  35. foreach ( (array)$ancestors as $ancestor ) { 
  36. $ancestor_term = get_term($ancestor, $taxonomy); 
  37. $hierarchical_slugs[] = $ancestor_term->slug; 
  38. $hierarchical_slugs = array_reverse($hierarchical_slugs); 
  39. $hierarchical_slugs[] = $slug; 
  40. $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink); 
  41. } else { 
  42. $termlink = str_replace("%$taxonomy%", $slug, $termlink); 
  43. $termlink = home_url( user_trailingslashit($termlink, 'category') ); 
  44. // Back Compat filters. 
  45. if ( 'post_tag' == $taxonomy ) { 
  46.  
  47. /** 
  48. * Filters the tag link. 
  49. * @since 2.3.0 
  50. * @deprecated 2.5.0 Use 'term_link' instead. 
  51. * @param string $termlink Tag link URL. 
  52. * @param int $term_id Term ID. 
  53. */ 
  54. $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); 
  55. } elseif ( 'category' == $taxonomy ) { 
  56.  
  57. /** 
  58. * Filters the category link. 
  59. * @since 1.5.0 
  60. * @deprecated 2.5.0 Use 'term_link' instead. 
  61. * @param string $termlink Category link URL. 
  62. * @param int $term_id Term ID. 
  63. */ 
  64. $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); 
  65.  
  66. /** 
  67. * Filters the term link. 
  68. * @since 2.5.0 
  69. * @param string $termlink Term link URL. 
  70. * @param object $term Term object. 
  71. * @param string $taxonomy Taxonomy slug. 
  72. */ 
  73. return apply_filters( 'term_link', $termlink, $term, $taxonomy );