函数文档

get_media_items()

💡 云策文档标注

概述

get_media_items() 函数用于获取文章图库中媒体项的 HTML 内容,主要用于 SWF Upload 组件的进度显示,并生成用于显示和隐藏修改图像附件表单的链接。

关键要点

  • 函数接受两个参数:$post_id(必需,文章 ID)和 $errors(必需,附件错误数组)。
  • 返回值为字符串,表示文章图库媒体项的 HTML 内容。
  • 函数逻辑:根据 $post_id 获取附件,如果 $post_id 对应附件类型,则直接处理;否则通过 get_children() 获取子附件;若无 $post_id,则从全局查询中获取附件。
  • 遍历附件时,跳过状态为 'trash' 的附件,并使用 get_media_item() 生成每个附件的 HTML 项。
  • 相关函数包括 get_children()、get_media_item() 和 get_post(),用于辅助数据获取。

代码示例

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 .= "npost_parent preloaded'>$itemn";
        }
    }

    return $output;
}

注意事项

  • 函数自 WordPress 2.5.0 版本引入,属于核心功能的一部分。
  • 主要用于后台媒体管理界面,如 media_upload_gallery_form() 等函数调用。
  • 处理附件时需确保 $errors 参数正确传递,以处理可能的附件错误。

📄 原文内容

Retrieves HTML for media items of post gallery.

Description

The HTML markup retrieved will be created for the progress of SWF Upload component. Will also create link for showing and hiding the form to modify the image attachment.

Parameters

$post_idintrequired
Post ID.
$errorsarrayrequired
Errors for attachment, if any.

Return

string HTML content for media items of post gallery.

Source

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;
}

Changelog

Version Description
2.5.0 Introduced.