WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get term link

Description

Returns permalink for a taxonomy term archive, or a WP_Error object if the term does not exist.

Usage

<?php get_term_link$term$taxonomy ); ?> 

Parameters

$term
(object/int/string) (required) The term object / term ID / term slug whose link will be retrieved.
Default: None
$taxonomy
(string) (optional) The taxonomy slug. Optional when $term is an object.
Default: empty string

Return Values

URL (string) 
URL to taxonomy term archive.
WP_Error (object) 
Error if term does not exist.

Examples

$terms = get_terms( 'species' );

echo '<ul>';

foreach ( $terms as $term ) {

    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );
   
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }

    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

echo '</ul>';

Notes

  • Uses: apply_filters() Calls 'term_link' filter on the finished link.
  • Uses global: (WP_Rewrite) $wp_rewrite
  • Since the term can be an object, integer, or string, make sure that any numbers you pass in are explicitly converted to an integer (example: (int) $term_id). Otherwise the function will assume that $term is a slug instead of a term ID.
  • Because of the above, $term->term_id doesn't work for the $term parameter prior to 3.8.0. See this trac ticket for more info.
  • PHP may halt if you attempt to print an error result ("Catchable fatal error: Object of class WP_Error could not be converted to string"). You should always use is_wp_error() to check the result of this function, in case the term does not exist.

Change Log

  • 3.1.0: $taxonomy is optional when passing a term object. It is still required if $term is passed as a string. See ticket #13081.
  • Since: 2.5.0

Source File

get_term_link() is located in wp-includes/taxonomy.php.

Related

See also index of Function Reference and index of Template Tags.