get_attached_media( string $type, int|WP_Post $post )
Retrieves media attached to the passed post.
描述
参数
$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 );
}
更新日志
Version
描述
3.6.0
Introduced.
相关函数
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
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 );
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…