我正在创建元框,将图片库附加到帖子中。但我保存后只得到一张图像。请更正下面的回调函数:
function save_custom_meta_data($id) {
/* --- security verification --- */
if(!wp_verify_nonce($_POST[\'wp_custom_attachment_nonce\'], plugin_basename(__FILE__))) {
return $id;
} // end if
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $id;
} // end if
if(\'page\' == $_POST[\'post_type\']) {
if(!current_user_can(\'edit_page\', $id)) {
return $id;
} // end if
} else {
if(!current_user_can(\'edit_page\', $id)) {
return $id;
} // end if
} // end if
/* - end security verification - */
// Make sure the file array isn\'t empty
foreach ($_FILES[\'wp_custom_attachment\']["tmp_name"] as $key => $value) {
if(!empty($_FILES[\'wp_custom_attachment\'][\'name\'][$key])) {
// Setup the array of supported file types. In this case, it\'s just PDF.
$supported_types = array(\'application/pdf\', \'image/jpeg\', \'image/png\', \'image/jpg\', \'image/gif\');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES[\'wp_custom_attachment\'][\'name\'][$key]));
$uploaded_type = $arr_file_type[\'type\'];
// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES[\'wp_custom_attachment\'][\'name\'][$key], null, file_get_contents($_FILES[\'wp_custom_attachment\'][\'tmp_name\'][$key]));
if(isset($upload[\'error\']) && $upload[\'error\'] != 0) {
wp_die(\'There was an error uploading your file. The error is: \' . $upload[\'error\']);
} else {
$upload提供所选多个图像的正确数组值。但我不确定add\\u post\\u meta和update\\u post\\u meta是否保存了多个图像。 add_post_meta($id, \'wp_custom_attachment\', $upload);
update_post_meta($id, \'wp_custom_attachment\', $upload);
} // end if/else
} else {
wp_die("The file type that you\'ve uploaded is not of supported type.");
} // end if/else
} // end if
}
} // end save_custom_meta_data
add_action(\'save_post\', \'save_custom_meta_data\');
我正在获取如下图像:$file = get_post_meta(get_the_ID(), \'wp_custom_attachment\', false);
var_dump($file)
;//给我的是错误的相同图像数组。我不知道如何检查是否保存了所有图像?它们是否被覆盖,这就是为什么我在使用get_post_meta
?