返回介绍

get_attachment_taxonomies()

发布于 2017-09-10 22:46:34 字数 3074 浏览 1043 评论 0 收藏 0

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

Retrieves taxonomies attached to given the attachment.


description


参数

$attachment

(int|array|object) (Required) Attachment ID, data array, or data object.

$output

(string) (Optional) Output type. 'names' to return an array of taxonomy names, or 'objects' to return an array of taxonomy objects. Default is 'names'.

Default value: 'names'


返回值

(array) Empty array on failure. List of taxonomies on success.


源代码

File: wp-includes/media.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;
}

更新日志

Versiondescription
4.7.0Introduced the $output parameter.
2.5.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: get_object_taxonomies()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_attached_file()

Used By

  • wp-admin/includes/media.php: get_attachment_fields_to_edit()
  • wp-admin/includes/media.php: get_compat_media_markup()
  • wp-admin/includes/media.php: media_upload_form_handler()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_compat()
  • wp-includes/taxonomy.php: get_object_taxonomies()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文