我已经在我的每个帖子上为缩略图设置了一个自定义帖子元字段。此函数被正确调用,并且直到update_post_meta的最后一行都正常工作
有趣的是,我可以回显$imageURL并获得正确的地址,并且文件上传得很好。我甚至可以对任何其他值进行update_post_meta,无论它是一个字符串,还是函数中的另一个变量,但只要我尝试使用$imageURL或$uploaded_file['url'],它就会将post元设置为空字符串。
我曾在3.1之前使用WordPress开发的项目中使用过此代码片段,但此版本是3.1。会不会跟这件事有关?我有点怀疑,因为这似乎是那些超级奇怪的bug之一。
function tcr_save_thumbnail($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if(!empty($_FILES['tcr_thumbnail_meta']['name'])) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$uploaded_file = wp_handle_upload($_FILES['tcr_thumbnail_meta'], $override);
$post_id = $post->ID;
$attachment = array(
'post_title' => $_FILES['tcr_thumbnail_meta']['name'],
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $_FILES['tcr_thumbnail_meta']['type'],
'guid' => $uploaded_file['url']
);
// Save the data
$id = wp_insert_attachment( $attachment,$_FILES['tcr_thumbnail_meta'][ 'file' ], $post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES['tcr_thumbnail_meta']['file'] ) );
$imageURL = $uploaded_file['url'];
update_post_meta($post->ID, "tcr_thumbnail_meta", $imageURL);
}
}发布于 2011-03-08 12:26:24
这是一个已经在插件中实现的功能。试试吧,http://wordpress.org/extend/plugins/taxonomy-images/,它太完整了!
https://stackoverflow.com/questions/5227966
复制相似问题