# get_term_link()

URL: https://chugunov.pro/api-wordpress/functions/get_term_link/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.5.0.

## Сигнатура

```php
get_term_link( WP_Term|int|string $term, string $taxonomy = '' ): string|WP_Error
```

## Описание

Генерирует постоянную ссылку для архива термина таксономии.

## Параметры

- `$term` `WP_Term|int|string` — обязательный. Объект термина, его ID или слаг, ссылку на который нужно получить.
- `$taxonomy` `string` — необязательный, по умолчанию `''`. Таксономия.

## Возвращаемое значение

`string|WP_Error` — WP_Error

## Исходный код

Файл: `wp-includes/taxonomy.php:4670`

```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 term 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 ( ! empty( $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
		 * @since 2.5.0 Deprecated in favor of 'term_link' filter.
		 * @since 5.4.1 Restored (un-deprecated).
		 *
		 * @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
		 * @since 2.5.0 Deprecated in favor of 'term_link' filter.
		 * @since 5.4.1 Restored (un-deprecated).
		 *
		 * @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 WP_Term $term     Term object.
	 * @param string  $taxonomy Taxonomy slug.
	 */
	return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
```

## История изменений

- 2.5.0 — Introduced.

## Связанные

Использует: [`get_ancestors`](https://chugunov.pro/api-wordpress/functions/get_ancestors/), [`get_term_by`](https://chugunov.pro/api-wordpress/functions/get_term_by/), [`user_trailingslashit`](https://chugunov.pro/api-wordpress/functions/user_trailingslashit/), `WP_Rewrite::get_extra_permastruct`, [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`get_term`](https://chugunov.pro/api-wordpress/functions/get_term/), [`get_taxonomy`](https://chugunov.pro/api-wordpress/functions/get_taxonomy/), [`home_url`](https://chugunov.pro/api-wordpress/functions/home_url/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/), `WP_Error::__construct`.
Используется в: [`_block_bindings_term_data_get_value`](https://chugunov.pro/api-wordpress/functions/_block_bindings_term_data_get_value/), `WP_REST_Term_Search_Handler::prepare_item`, `WP_Sitemaps_Taxonomies::get_url_list`, [`get_term_parents_list`](https://chugunov.pro/api-wordpress/functions/get_term_parents_list/), `WP_REST_Terms_Controller::prepare_item_for_response`, `WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item`, `WP_Post`, `WP_Customize_Nav_Menus::search_available_items_query`, `WP_Customize_Nav_Menus::load_available_items_query`, `WP_Terms_List_Table::handle_row_actions`, `Walker_Category::start_el`, [`get_the_term_list`](https://chugunov.pro/api-wordpress/functions/get_the_term_list/), [`wp_tag_cloud`](https://chugunov.pro/api-wordpress/functions/wp_tag_cloud/), [`get_category_link`](https://chugunov.pro/api-wordpress/functions/get_category_link/), [`get_the_taxonomies`](https://chugunov.pro/api-wordpress/functions/get_the_taxonomies/), [`get_term_feed_link`](https://chugunov.pro/api-wordpress/functions/get_term_feed_link/), [`wp_admin_bar_edit_menu`](https://chugunov.pro/api-wordpress/functions/wp_admin_bar_edit_menu/), [`redirect_canonical`](https://chugunov.pro/api-wordpress/functions/redirect_canonical/), [`get_post_format_link`](https://chugunov.pro/api-wordpress/functions/get_post_format_link/), [`wp_setup_nav_menu_item`](https://chugunov.pro/api-wordpress/functions/wp_setup_nav_menu_item/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/get_term_link/
