多亏了推特上的一位朋友,我才得以实现这一目标。下面是代码。
function wpse_110060_image_sizes_js( $response, $attachment, $meta ){
$size_array = array( \'custom_size_one\', \'custom_size_two\') ;
foreach ( $size_array as $size ):
if ( isset( $meta[\'sizes\'][ $size ] ) ) {
$attachment_url = wp_get_attachment_url( $attachment->ID );
$base_url = str_replace( wp_basename( $attachment_url ), \'\', $attachment_url );
$size_meta = $meta[\'sizes\'][ $size ];
$response[\'sizes\'][ $size ] = array(
\'height\' => $size_meta[\'height\'],
\'width\' => $size_meta[\'width\'],
\'url\' => $base_url . $size_meta[\'file\'],
\'orientation\' => $size_meta[\'height\'] > $size_meta[\'width\'] ? \'portrait\' : \'landscape\',
);
}
endforeach;
return $response;
}
add_filter ( \'wp_prepare_attachment_for_js\', \'wpse_110060_image_sizes_js\' , 10, 3 );
注意:数组和foreach是必需的,因为我需要包含两个单独的数组。如果只有1个要包含,则可以删除。