^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_attached_media()用法示例

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

Retrieves media attached to the passed post.


描述


参数

$type

(string)
(Required)
Mime type.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$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()

  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…

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈