在这种情况下,我会将id与URL一起保存在一个隐藏的输入中。
function meta_image_upload() {
global $post;
$my_image = get_post_meta($post->ID, \'_my_image_metabox\', true);
$my_image_id = get_post_meta($post->ID, \'_my_image_id_metabox\', true);
$out = \'<input id="my_image" type="text" name="my_uploaded_image" value="\'. $my_image .\'">\';
$out .= \'<input id="my_image_id" type="hidden" name="my_uploaded_image_id" value="\'. $my_image_id .\'">\';
$out .= \'<button id="my_image_upload">Upload Image</button>\';
echo $out;
}
在上载处理程序脚本上:
attachment = custom_uploader.state().get(\'selection\').first().toJSON();
$(\'#my_image\').val(attachment.url);
$(\'#my_image_id\').val(attachment.id); // Just add this line
然后在其他回调时:
global $post;
$my_image_id = get_post_meta($post->ID, \'_my_image_id_metabox\', true);
wp_get_attachment_image_src($my_image_id, \'desired-size\');
这就是我的解决方案,也许有人会想出更好的解决方案。然而,我发现它比查询数据库来获取关联的附件id更快。