# get_attachment_link()

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

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

## Сигнатура

```php
get_attachment_link( int|WP_Post $post = null, bool $leavename = false ): string
```

## Описание

Можно использовать как внутри цикла WordPress, так и вне его.

## Параметры

- `$post` `int|WP_Post` — необязательный, по умолчанию `null`. ID записи или объект. По умолчанию используется глобальная переменная $post.
- `$leavename` `bool` — необязательный, по умолчанию `false`. Сохранять ли имя страницы.

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

`string`

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

Файл: `wp-includes/link-template.php:472`

```php
function get_attachment_link( $post = null, $leavename = false ) {
	global $wp_rewrite;

	$link = false;

	$post             = get_post( $post );
	$force_plain_link = wp_force_plain_post_permalink( $post );
	$parent_id        = $post->post_parent;
	$parent           = $parent_id ? get_post( $parent_id ) : false;
	$parent_valid     = true; // Default for no parent.
	if (
		$parent_id &&
		(
			$post->post_parent === $post->ID ||
			! $parent ||
			! is_post_type_viewable( get_post_type( $parent ) )
		)
	) {
		// Post is either its own parent or parent post unavailable.
		$parent_valid = false;
	}

	if ( $force_plain_link || ! $parent_valid ) {
		$link = false;
	} elseif ( $wp_rewrite->using_permalinks() && $parent ) {
		if ( 'page' === $parent->post_type ) {
			$parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
		} else {
			$parentlink = get_permalink( $post->post_parent );
		}

		if ( is_numeric( $post->post_name ) || str_contains( get_option( 'permalink_structure' ), '%category%' ) ) {
			$name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker.
		} else {
			$name = $post->post_name;
		}

		if ( ! str_contains( $parentlink, '?' ) ) {
			$link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' );
		}

		if ( ! $leavename ) {
			$link = str_replace( '%postname%', $name, $link );
		}
	} elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
		$link = home_url( user_trailingslashit( $post->post_name ) );
	}

	if ( ! $link ) {
		$link = home_url( '/?attachment_id=' . $post->ID );
	}

	/**
	 * Filters the permalink for an attachment.
	 *
	 * @since 2.0.0
	 * @since 5.6.0 Providing an empty string will now disable
	 *              the view attachment page link on the media modal.
	 *
	 * @param string $link    The attachment's permalink.
	 * @param int    $post_id Attachment ID.
	 */
	return apply_filters( 'attachment_link', $link, $post->ID );
}
```

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

- 2.0.0 — Introduced.

## Связанные

Использует: [`wp_force_plain_post_permalink`](https://chugunov.pro/api-wordpress/functions/wp_force_plain_post_permalink/), [`is_post_type_viewable`](https://chugunov.pro/api-wordpress/functions/is_post_type_viewable/), [`_get_page_link`](https://chugunov.pro/api-wordpress/functions/_get_page_link/), [`user_trailingslashit`](https://chugunov.pro/api-wordpress/functions/user_trailingslashit/), [`get_post_type`](https://chugunov.pro/api-wordpress/functions/get_post_type/), `WP_Rewrite::using_permalinks`, [`trailingslashit`](https://chugunov.pro/api-wordpress/functions/trailingslashit/), [`home_url`](https://chugunov.pro/api-wordpress/functions/home_url/), [`get_permalink`](https://chugunov.pro/api-wordpress/functions/get_permalink/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_option`](https://chugunov.pro/api-wordpress/functions/get_option/), [`get_post`](https://chugunov.pro/api-wordpress/functions/get_post/).
Используется в: `WP_Widget_Media_Image::render_media`, [`image_media_send_to_editor`](https://chugunov.pro/api-wordpress/functions/image_media_send_to_editor/), [`media_upload_form_handler`](https://chugunov.pro/api-wordpress/functions/media_upload_form_handler/), [`image_link_input_fields`](https://chugunov.pro/api-wordpress/functions/image_link_input_fields/), [`_fix_attachment_links`](https://chugunov.pro/api-wordpress/functions/_fix_attachment_links/), [`wp_ajax_send_attachment_to_editor`](https://chugunov.pro/api-wordpress/functions/wp_ajax_send_attachment_to_editor/), [`get_the_attachment_link`](https://chugunov.pro/api-wordpress/functions/get_the_attachment_link/), [`get_permalink`](https://chugunov.pro/api-wordpress/functions/get_permalink/), [`wp_get_attachment_link`](https://chugunov.pro/api-wordpress/functions/wp_get_attachment_link/), [`wp_prepare_attachment_for_js`](https://chugunov.pro/api-wordpress/functions/wp_prepare_attachment_for_js/), [`redirect_canonical`](https://chugunov.pro/api-wordpress/functions/redirect_canonical/).

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