WordPress at a glance

get_term_link() WP 2.5.0

Gets the URL for the archive page of a term (taxonomy item). The same as the link to the category page.

The function returns a permanent link to the page of a particular taxonomy element (term).

The function is useful when a custom taxonomy is created and you need to get a link to any element of this custom taxonomy.

If you pass a term ID to the $term parameter, make sure that you pass a number (int) type. For example, if you pass $term_id variable, process it like so (int) $trem_id. If the variable contains a number string '5', the function will look for a term with a slug 5, and not term with ID 5 - this can cause an incomprehensible error.

  • Global. WP_Rewrite. $wp_rewrite
Hooks in function
Return

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

Usage

get_term_link( $term, $taxonomy );
$term(object/int/string) (required)
The term object, ID, or slug whose link will be retrieved.
$taxonomy(string)
Taxonomy.
Default: ''

Examples

#1 Display a link to the archive page of the taxonomy item

5 is ID of the taxonomy item, tax_name is the name of the taxonomy.

$term_id = 5;
$term_link = get_term_link( $term_id, 'tax_name' );
echo '<a href="'. $term_link .'">link to section with ID '. $term_id .'</a>';

#2 $term_id must be an int type if it is an ID (numeric)...

Suppose that $term_id is already known and passed as a string, then it must be converted to an integer, otherwise, the function will interpret it as a term slug, not as a term ID.

$term_id = '5';
$term_link = get_term_link( (int) $term_id, 'tax_name' );
echo '<a href="'. $term_link .'">link to section with ID '. $term_id .'</a>';

#3 Display the link of the term by its name

$term_slug = 'other'; // you need to pass a slug
$term_link = get_term_link( $term_slug, 'tax_name' );
echo '<a href="'. $term_link .'">link to section '. $term_slug .'</a>';

Code of get term link: wp-includes/taxonomy.php WP 5.2.1

<?php
function get_term_link( $term, $taxonomy = '' ) {
	global $wp_rewrite;

	if ( ! is_object( $term ) ) {
		if ( is_int( $term ) ) {
			$term = get_term( $term, $taxonomy );
		} else {
			$term = get_term_by( 'slug', $term, $taxonomy );
		}
	}

	if ( ! is_object( $term ) ) {
		$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
	}

	if ( is_wp_error( $term ) ) {
		return $term;
	}

	$taxonomy = $term->taxonomy;

	$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );

	/**
	 * Filters the permalink structure for a terms before token replacement occurs.
	 *
	 * @since 4.9.0
	 *
	 * @param string  $termlink The permalink structure for the term's taxonomy.
	 * @param WP_Term $term     The term object.
	 */
	$termlink = apply_filters( 'pre_term_link', $termlink, $term );

	$slug = $term->slug;
	$t    = get_taxonomy( $taxonomy );

	if ( empty( $termlink ) ) {
		if ( 'category' == $taxonomy ) {
			$termlink = '?cat=' . $term->term_id;
		} elseif ( $t->query_var ) {
			$termlink = "?$t->query_var=$slug";
		} else {
			$termlink = "?taxonomy=$taxonomy&term=$slug";
		}
		$termlink = home_url( $termlink );
	} else {
		if ( $t->rewrite['hierarchical'] ) {
			$hierarchical_slugs = array();
			$ancestors          = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
			foreach ( (array) $ancestors as $ancestor ) {
				$ancestor_term        = get_term( $ancestor, $taxonomy );
				$hierarchical_slugs[] = $ancestor_term->slug;
			}
			$hierarchical_slugs   = array_reverse( $hierarchical_slugs );
			$hierarchical_slugs[] = $slug;
			$termlink             = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
		} else {
			$termlink = str_replace( "%$taxonomy%", $slug, $termlink );
		}
		$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
	}
	// Back Compat filters.
	if ( 'post_tag' == $taxonomy ) {

		/**
		 * Filters the tag link.
		 *
		 * @since 2.3.0
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @param string $termlink Tag link URL.
		 * @param int    $term_id  Term ID.
		 */
		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
	} elseif ( 'category' == $taxonomy ) {

		/**
		 * Filters the category link.
		 *
		 * @since 1.5.0
		 * @deprecated 2.5.0 Use 'term_link' instead.
		 *
		 * @param string $termlink Category link URL.
		 * @param int    $term_id  Term ID.
		 */
		$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
	}

	/**
	 * Filters the term link.
	 *
	 * @since 2.5.0
	 *
	 * @param string $termlink Term link URL.
	 * @param object $term     Term object.
	 * @param string $taxonomy Taxonomy slug.
	 */
	return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}

Related Functions

From tag: permalinks (clean friendly URL)

More from tag: Term (taxonomies terms)

More from category: Any taxonomy

No comments
    Hello, !     Log In . Register