我正在构建一种在前端上传多个文件以用于公告自定义帖子类型的方法。我的功能在此成功上传任意数量的文件,以便我可以在WordPress的“媒体”下查看它们。。。附在岗位上。
我需要能够将文件的url存储在自定义字段中。当我使用update_post_meta
... 它只更新下面我的foreach循环中最后一个循环/文件的值。我不明白为什么?
update_post_meta()
表示“如果meta不存在,则返回meta\\u id;否则,如果成功,则返回true;如果提交的值与数据库中已有的值相同,则返回false。”http://codex.wordpress.org/Function_Reference/update_post_meta
在代码中查看我的注释以了解发生的情况。
有什么想法吗?谢谢
akpsi_insert_attachment( $announcement_id, \'wpcf-announcement_attached_file\' );
function akpsi_insert_attachment( $post_id, $post_meta ) {
if ( $_FILES[\'announcement_upload\'] ) {
$files = $_FILES[\'announcement_upload\'];
foreach ( $files[\'name\'] as $key => $value ) {
if ( $files[\'name\'][$key] ) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array( "announcement_upload" => $file );
foreach ( $_FILES as $file_key => $array ) {
// check to make sure its a successful upload
if ($_FILES[$file_key] !== UPLOAD_ERR_OK) __return_false();
require_once( ABSPATH . "wp-admin" . \'/includes/image.php\' );
require_once( ABSPATH . "wp-admin" . \'/includes/file.php\' );
require_once( ABSPATH . "wp-admin" . \'/includes/media.php\' );
// successfully uploads any file on each loop
$attach_id = media_handle_upload( $file_key, $post_id );
// this is only updating post meta once, and only the last loop/file.
$attach_url = wp_get_attachment_url( $attach_id );
$postmeta_updated = update_post_meta( $post_id, $post_meta, $attach_url );
// test to see what it gives me on each loop...
// ex: gives me \'int(777)\' for first loop/file, \'bool(true)\' for second loop/file, \'bool(true)\' for third loop/file
var_dump( $postmeta_updated );
}
}
}
}
}