首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress上传自定义帖子元的缩略图

WordPress上传自定义帖子元的缩略图
EN

Stack Overflow用户
提问于 2011-03-08 11:24:58
回答 1查看 995关注 0票数 0

我已经在我的每个帖子上为缩略图设置了一个自定义帖子元字段。此函数被正确调用,并且直到update_post_meta的最后一行都正常工作

有趣的是,我可以回显$imageURL并获得正确的地址,并且文件上传得很好。我甚至可以对任何其他值进行update_post_meta,无论它是一个字符串,还是函数中的另一个变量,但只要我尝试使用$imageURL$uploaded_file['url'],它就会将post元设置为空字符串。

我曾在3.1之前使用WordPress开发的项目中使用过此代码片段,但此版本是3.1。会不会跟这件事有关?我有点怀疑,因为这似乎是那些超级奇怪的bug之一。

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-03-08 12:26:24

这是一个已经在插件中实现的功能。试试吧,http://wordpress.org/extend/plugins/taxonomy-images/,它太完整了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5227966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档