get_media_items

函数
get_media_items ( $post_id, $errors )
参数
  • (int) $post_id Post ID.
    Required:
  • (array) $errors Errors for attachment, if any.
    Required:
返回值
  • (string) HTML content for media items of post gallery.
定义位置
相关方法
get_media_itemget_media_states_media_statesget_the_termsget_meta_keys
引入
2.5.0
弃用
-

get_media_items函数用于检索一个数组的媒体项目(如图片、视频),这些媒体项目附在文章或页面上: 这个函数可以用来显示与一个文章或页面相关的所有媒体项目。

检索文章库的媒体项目的HTML。

检索到的HTML标记将被创建为SWF上传组件的进度。也将创建链接,以显示和隐藏修改图片附件的表格。

function get_media_items( $post_id, $errors ) {
	$attachments = array();

	if ( $post_id ) {
		$post = get_post( $post_id );

		if ( $post && 'attachment' === $post->post_type ) {
			$attachments = array( $post->ID => $post );
		} else {
			$attachments = get_children(
				array(
					'post_parent' => $post_id,
					'post_type'   => 'attachment',
					'orderby'     => 'menu_order ASC, ID',
					'order'       => 'DESC',
				)
			);
		}
	} else {
		if ( is_array( $GLOBALS['wp_the_query']->posts ) ) {
			foreach ( $GLOBALS['wp_the_query']->posts as $attachment ) {
				$attachments[ $attachment->ID ] = $attachment;
			}
		}
	}

	$output = '';
	foreach ( (array) $attachments as $id => $attachment ) {
		if ( 'trash' === $attachment->post_status ) {
			continue;
		}

		$item = get_media_item( $id, array( 'errors' => isset( $errors[ $id ] ) ? $errors[ $id ] : null ) );

		if ( $item ) {
			$output .= "n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress hidden'><div class='bar'></div></div><div id='media-upload-error-$id' class='hidden'></div><div class='filename hidden'></div>$itemn</div>";
		}
	}

	return $output;
}

常见问题

FAQs
查看更多 >