【问题标题】:WordPress Uploading Thumbnail for Custom Post MetaWordPress 上传自定义帖子元的缩略图
【发布时间】:2011-03-08 03:24:57
【问题描述】:

我已经为我的每篇帖子的缩略图设置了一个自定义帖子元字段。该函数被正确调用,一切正常,直到update_post_meta的最后一行

有趣的是,我可以回显$imageURL 并获得正确的地址,并且文件上传正常。我什至可以将update_post_meta 与任何其他值一起使用,无论它是一个字符串,还是函数中的另一个变量,但是一旦我尝试使用$imageURL$uploaded_file['url'],它只会将帖子元设置为空白字符串。

我在使用 WordPress 3.1 之前开发的项目中使用了这个 sn-p,但这个是 3.1。这可能与它有关吗?我有点怀疑,因为这似乎是那些超级奇怪的错误之一。

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);
    }
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多