返回介绍

get_attached_media()

发布于 2017-09-10 22:44:57 字数 3665 浏览 1075 评论 0 收藏 0

get_attached_media( string $type,  int|WP_Post $post )

Retrieves media attached to the passed post.


description


参数

$type

(string) (Required) Mime type.

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.


返回值

(array) Found attachments.


源代码

File: wp-includes/media.php

function get_attached_media( $type, $post = 0 ) {
	if ( ! $post = get_post( $post ) )
		return array();

	$args = array(
		'post_parent' => $post->ID,
		'post_type' => 'attachment',
		'post_mime_type' => $type,
		'posts_per_page' => -1,
		'orderby' => 'menu_order',
		'order' => 'ASC',
	);

	/**
	 * Filters arguments used to retrieve media attached to the given post.
	 *
	 * @since 3.6.0
	 *
	 * @param array  $args Post query arguments.
	 * @param string $type Mime type of the desired media.
	 * @param mixed  $post Post ID or object.
	 */
	$args = apply_filters( 'get_attached_media_args', $args, $type, $post );

	$children = get_children( $args );

	/**
	 * Filters the list of media attached to the given post.
	 *
	 * @since 3.6.0
	 *
	 * @param array  $children Associative array of media attached to the given post.
	 * @param string $type     Mime type of the media desired.
	 * @param mixed  $post     Post ID or object.
	 */
	return (array) apply_filters( 'get_attached_media', $children, $type, $post );
}

更新日志

Versiondescription
3.6.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_children()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: get_attached_media
  • wp-includes/media.php: get_attached_media_args
  • wp-includes/post.php: get_post()

Used By

  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/media.php: wp_audio_shortcode()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Jules Colle

    You can get all attached media, regardless of type, by passing an empty string:

    $media = get_attached_media( '' );

    or

    $media = get_attached_media( '', 102 );
  2. Examples
    Get image attachment(s) to the current Post:

    
    $media = get_attached_media( 'image' );
    

    Get attachment(s) of mime-type ‘audio’ to the Post with an ID of 102:

    
    $media = get_attached_media( 'audio', 102 );
    
    

    It should be noted, that this function returns array of WP_Post objects, indexed by their ID, ordered by their “menu order” by default – that is where usually plugins handling attachment store their order positions…

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

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

发布评论

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