检索附加给定附件的分类法。

原型

get_attachment_taxonomies( int|array|object $attachment, string $output = 'names' )

参数

$attachment (int|array|object) (Required) 附件ID,数据数组或数据对象。

$output (string) (Optional) 输出类型。 ‘names’返回一系列分类名称,或’objects’返回一系列分类对象。默认为’名称’。

返回值

(array)  失败时空阵列。成功分类的清单。

源文件

路径:wp-includes/media.php

<?php
...
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 = basename( $file );
	$objects = array('attachment');
	if ( false !== strpos($filename, '.') )
		$objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
	if ( !empty($attachment->post_mime_type) ) {
		$objects[] = 'attachment:' . $attachment->post_mime_type;
		if ( false !== strpos($attachment->post_mime_type, '/') )
			foreach ( explode('/', $attachment->post_mime_type) as $token )
				if ( !empty($token) )
					$objects[] = "attachment:$token";
	}
	$taxonomies = array();
	foreach ( $objects as $object ) {
		if ( $taxes = get_object_taxonomies( $object, $output ) ) {
			$taxonomies = array_merge( $taxonomies, $taxes );
		}
	}
	if ( 'names' === $output ) {
		$taxonomies = array_unique( $taxonomies );
	}
	return $taxonomies;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_attachment_taxonomies/