функция
since 4.7.0
get_attachment_taxonomies()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_attachment_taxonomies( int|array|object $attachment, string $output = 'names' ): string[]|WP_Taxonomy[]
Описание
Получает таксономии, привязанные к указанному вложению.
Параметры
$attachment
int|array|object
обязательный
Идентификатор вложения, массив данных или объект данных.
$output
string
необязательный
= 'names'
Тип вывода. 'names' — вернуть массив имён таксономий, 'objects' — вернуть массив объектов таксономий.
Значение по умолчанию — 'names'.
Возвращаемое значение
string[]|WP_Taxonomy[]
Исходный код
wp-includes/media.php:4024
function get_attachment_taxonomies( $attachment, $output = 'names' ) {
if ( is_int( $attachment ) ) {
$attachment = get_post( $attachment );
} elseif ( is_array( $attachment ) ) {
$attachment = (object) $attachment;
}
if ( ! is_object( $attachment ) ) {
return array();
}
$file = get_attached_file( $attachment->ID );
$filename = wp_basename( $file );
$objects = array( 'attachment' );
if ( str_contains( $filename, '.' ) ) {
$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 );
}
if ( ! empty( $attachment->post_mime_type ) ) {
$objects[] = 'attachment:' . $attachment->post_mime_type;
if ( str_contains( $attachment->post_mime_type, '/' ) ) {
foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
if ( ! empty( $token ) ) {
$objects[] = "attachment:$token";
}
}
}
}
$taxonomies = array();
foreach ( $objects as $object ) {
$taxes = get_object_taxonomies( $object, $output );
if ( $taxes ) {
$taxonomies = array_merge( $taxonomies, $taxes );
}
}
if ( 'names' === $output ) {
$taxonomies = array_unique( $taxonomies );
}
return $taxonomies;
}
История изменений
| Версия | Описание |
|---|---|
| 4.7.0 | Introduced the $output parameter. |
| 2.5.0 | Introduced. |

