我几天来一直在努力解决这个问题。。我用尽了所有的选择,因此需要一些新的想法。。我有一个页面模板,用于从wordpress前端发布/编辑2种不同的自定义帖子类型。
为了简单起见,这里我将这些自定义帖子类型命名为post\\u type\\u x和post\\u type\\u y。可以编辑这些帖子类型的页面名称将分别为page\\u x和page\\u y,并且这两个页面都被指定使用相同的模板。因此,模板页面的代码结构如下:
<?php
if (is_page(\'page_x\')) {
$current_post_type = "post_type_x";
some if statements to determine the $action variable according to the $_POST variables
switch ($action) {
case "...":
.... break;
case "update_save":
.... break;
}
} elseif (is_page(\'page_y\')) {
$current_post_type = "post_type_y";
some if statements to determine the $action variable according to the $_POST variables
switch ($action) {
case "...":
.... break;
case "update_save":
.... break;
}
} else { ...
}
....
get_header(); ?>
....
<form action="<?php the_permalink(); ?>" method="post" name="post_adv_resp_form" enctype="multipart/form-data">
....
<input type="file" name="file-attachment" id="file-attachment" />
....
</form>
....
在“update\\u save”情况下,有这样一个代码:
case "update_save":
$post_id = update_post_of_custom_type($_POST[\'post_id\'],$post_title,$post_content,\'draft\');
if ($post_id != false) {
$file_upload = upload_file_of_custom_type($_FILES[\'file-attachment\'],$post_id,\'file-attachment\');
...
} break;
问题是:当我在page\\u x上进行测试时,“update\\u save”案例非常有效。upload\\u file\\u of\\u custom\\u type函数上载文件,更新post meta。但是,当我在page\\y上时,脚本上载文件,然后中断,不会更新post meta。我有一个白色的屏幕。尽管调试模式已打开,但屏幕上不会显示任何错误。当我检查apache日志时,我什么也没发现。。
function upload_file_of_custom_type($files,$post_id,$file_form_handler) {
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
... some controls ...
$attach_id = media_handle_upload( $file_form_handler, $post_id, array( \'test_form\' => false ) ); $notice = "attach_id: ".$attach_id."upload is OK";
if ($attach_id != false){
$file_load = update_post_meta($post_id,\'_wp_attached_file\',$attach_id);
if ($file_load == false) {
$notice = __(\'An error occured while setting the post meta for the uploaded file.\', TDOMAIN);
} elseif ($file_load == true) {
$notice = \'OK\';
}
} else {
$notice = __(\'An error occured while uploading the file.\', TDOMAIN);
}
return $notice;
}
还有一个信息。。post\\u type\\u y作为post\\u type\\u y的子页发布。。我不知道这是否有什么区别。。关于问题可能是什么,有什么想法吗?
最合适的回答,由SO网友:ugurcem 整理而成
我对这个问题稍作休息,今天以一种全新的心态把它还给了我。。我注意到问题是关于文件上传功能。。我修改了代码如下:
$attach_id = media_handle_upload( $file_form_handler, $post_id );
if ($attach_id != false){ // if the attachment is loaded, register them as post meta
$file_load = update_post_meta($attach_id,\'_wp_attached_file\',$files[\'name\']);
$attach_data = wp_generate_attachment_metadata( $attach_id, ABSPATH."media/".$files[\'name\'] );
$file_load = wp_update_attachment_metadata( $attach_id, $attach_data );
if ($file_load == false) {
$notice = __(\'An error occured while setting the post meta for the uploaded file.\', TDOMAIN);
} elseif ($file_load == true) {
$notice = \'OK\';
}
} else {
$notice = __(\'An error occured while uploading the file.\', TDOMAIN);
}