考虑查看wp_prepare_attachment_for_js( $attachment )
, 哪里$attachment
是附件本身的WP\\u Post对象。
这是一个“厨房水槽”函数,但它确实提供了一个非常好的哈希,包含大量元数据,包括“alt”:
$response = array(
\'id\' => $attachment->ID,
\'title\' => $attachment->post_title,
\'filename\' => wp_basename( $attachment->guid ),
\'url\' => $attachment_url,
\'link\' => get_attachment_link( $attachment->ID ),
\'alt\' => get_post_meta( $attachment->ID, \'_wp_attachment_image_alt\', true ),
\'author\' => $attachment->post_author,
\'description\' => $attachment->post_content,
\'caption\' => $attachment->post_excerpt,
\'name\' => $attachment->post_name,
\'status\' => $attachment->post_status,
\'uploadedTo\' => $attachment->post_parent,
\'date\' => strtotime( $attachment->post_date_gmt ) * 1000,
\'modified\' => strtotime( $attachment->post_modified_gmt ) * 1000,
\'menuOrder\' => $attachment->menu_order,
\'mime\' => $attachment->post_mime_type,
\'type\' => $type,
\'subtype\' => $subtype,
\'icon\' => wp_mime_type_icon( $attachment->ID ),
\'dateFormatted\' => mysql2date( get_option(\'date_format\'), $attachment->post_date ),
\'nonces\' => array(
\'update\' => false,
\'delete\' => false,
\'edit\' => false
),
\'editLink\' => false,
\'meta\' => false,
);
这对于将附件图像元发送到wp特别有用(顾名思义)。媒体视图通过
wp_send_ajax()
, 但这并不意味着你不能将其用于其他目的。
我喜欢从_wp_attachment_image_alt
post meta字段,以防检索alt文本的方法发生变化(不太可能,但可以想象)。
我确实觉得wp_get_attachment_image_alt()
方法。