WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get term link

Contents

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) (required) The taxonomy slug. NOT required if you pass the term object in the first parameter
Default: None

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 ) {

    // Sanitize the term, since we will be displaying it.
    $term = sanitize_term( $term, 'species' );

    $term_link = get_term_link( $term, 'species' );
   
    // 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: (unknown) $wp_rewrite
  • Since the term can be an object, int, or string make sure that any numbers you pass in are explicitly converted to an integer. (int)$myID
  • term->term_id doesn't currently work for the $term parameter. 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"). Use is_string() to check the result of this function if you are not sure the term exists.

Change Log

Source File

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

Related

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