上传的图片未显示在自定义帖子类型中

时间:2013-10-16 作者:codecowboy

我正在写一个插件,它应该允许用户创建一个称为经销商的自定义帖子类型,并附加一个图像。图像正在上载到uploads文件夹,并以空白缩略图显示在媒体库中。该图像在后端的“经销商”列表中也不可见,即自定义帖子类型的管理界面。

有人能看到我错过了什么吗?相关功能从第362行开始:

https://gist.github.com/codecowboy/80fd6363c58558a74c9e

相关章节:

   public function wps_reseller_cpt() {
        $labels = array(
            \'name\'               => _x( \'WPS Resellers\', \'post type general name\' ),
            \'singular_name\'      => _x( \'WPS Reseller\', \'post type singular name\' ),
            \'add_new\'            => _x( \'Add New\', \'book\' ),
            \'add_new_item\'       => __( \'Add New Reseller\' ),
            \'edit_item\'          => __( \'Edit Reseller\' ),
            \'new_item\'           => __( \'New Reseller\' ),
            \'all_items\'          => __( \'All Resellers\' ),
            \'view_item\'          => __( \'View Reseller\' ),
            \'search_items\'       => __( \'Search Resellers\' ),
            \'not_found\'          => __( \'No resellers found\' ),
            \'not_found_in_trash\' => __( \'No Reseller found in the Trash\' ),
            \'parent_item_colon\'  => \'\',
            \'menu_name\'          => \'Resellers\'
        );
        $args = array(
            \'labels\'        => $labels,
            \'description\'   => \'Holds our resellers and reseller specific data\',
            \'public\'        => true,
            \'menu_position\' => 50,
            \'supports\'      => array( \'title\', \'editor\',\'image\', \'thumbnail\', \'custom-fields\' ),
            \'has_archive\'   => false,
        );
        register_post_type( \'wps-reseller\', $args );
    }

    public function create_reseller_profile_form(){

        require_once plugin_dir_path( __FILE__ ) . \'views/public-create_reseller_profile_form.php\';

    }

    public function intercept_reseller_profile_form() {

        if ( !empty( $_POST[\'wps-reseller-user-submission\'] ) ) {
            $this->wps_reseller_process_form();
        } else {
            return $template;
        }

    }

    public function wps_reseller_process_form() {
        require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');


        if ( wp_verify_nonce( $_POST[\'reseller_user_form\'], \'add_reseller_form\') &&
            !empty( $_POST[\'reseller-title\'] ) &&
            !empty( $_POST[\'reseller-description\'] ) &&
            !empty( $_FILES[\'reseller-image\']) )
        {

            //var_dump($_POST); exit;
            $file = $_FILES[\'reseller-image\'];
            $uploads = wp_upload_dir();//var_dump($uploads); exit;
            $new_reseller_profile_data = array(
                \'post_status\' => \'draft\',
                \'post_title\' => $_POST[\'reseller-title\'],
                \'post_content\' => $_POST[\'reseller-description\'],
                \'post_type\' => \'wps-reseller\'
            );

            $file_errors = $this->wps_parse_file_errors($file);
            $upload_overrides = array( \'test_form\' => FALSE );
            if($file_errors[\'error\'] == 0) {

                if($new_reseller_id = wp_insert_post( $new_reseller_profile_data )){

                    //$this->wps_process_image($file, $new_reseller_id);exit;

                    $uploaded_file = wp_handle_upload( $file, $upload_overrides );


                    $wp_filetype = wp_check_filetype( basename( $uploaded_file[\'file\'] ), null );
                    $attachment = array(
                        \'post_mime_type\' => $wp_filetype[\'type\'],
                        \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $uploaded_file[\'file\'] ) ),
                        \'post_content\' => \'\',
                        \'post_author\' => \'\',
                        \'post_status\' => \'inherit\',
                        \'post_type\' => \'attachment\',
                        \'post_parent\' => $new_reseller_id,
                        \'guid\' => $uploads[\'baseurl\'] . $uploads[\'subdir\'] . \'/\' . $file[\'name\']
                    );
echo $uploads[\'baseurl\'].\'<br />\';
echo $file[\'name\'].\'<br />\';
var_dump($uploads); exit;

                    $attachment_id = wp_insert_post( $attachment );
                    $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file[\'file\'] );

                    // update the attachment metadata
                    wp_update_attachment_metadata( $attachment_id,  $attach_data );
                    set_post_thumbnail( $new_reseller_id, $attachment_id );

                }
            }

        }

        //create a new reseller post, make it draft (ensure post type supports this)
        //email an admin user

    }

    protected function wps_parse_file_errors($file){

        define(\'MAX_UPLOAD_SIZE\', 200000);
        define(\'TYPE_WHITELIST\', serialize(array(
            \'image/jpeg\',
            \'image/png\',
            \'image/gif\'
        )));

        $result = array();
        $result[\'error\'] = 0;

        if($file[\'error\']){

            $result[\'error\'] = "No file uploaded or there was an upload error!";

            return $result;

        }
        $image_data = getimagesize($file[\'tmp_name\']);

        if(!in_array($image_data[\'mime\'], unserialize(TYPE_WHITELIST))){

            $result[\'error\'] = \'Your image must be a jpeg, png or gif!\';

        }elseif(($file[\'size\'] > MAX_UPLOAD_SIZE)){

            $result[\'error\'] = \'Your image was \' . $file[\'size\'] . \' bytes! It must not exceed \' . MAX_UPLOAD_SIZE . \' bytes.\';

        }
        return $result;

    }


}
问题:

为什么媒体库中的缩略图为空?下面是正在加载的内容:

如何使图像显示在管理界面中如果我使用媒体管理器将文件添加到库中,缩略图和图像将按预期显示。

如果相关,这是multisite 安装

1 个回复
SO网友:Kumar

你可以试试这个代码作为你的附件吗

 $uploaded_file = wp_handle_upload( $file, $upload_overrides );
 $attachment = array(
        \'post_mime_type\' => $uploaded_file[\'type\'],
        \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $uploaded_file[\'file\'] ) ),
        \'post_content\' => \'\',
        \'post_author\' => \'\',
        \'post_status\' => \'inherit\',
        \'post_type\' => \'attachment\',
        \'post_parent\' => $new_reseller_id,
        \'guid\' => $uploaded_file[\'file\']
    );
 $attachment_id = wp_insert_attachment( $attachment, $uploaded_file[\'file\'] );
 $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file[\'file\'] );

 // update the attachment metadata
 wp_update_attachment_metadata( $attachment_id,  $attach_data );
 //Set as thumbnail
 set_post_thumbnail ($new_reseller_id, $attachment_id );
获取帖子缩略图

如果你在循环中,那么

  the_post_thumbnail
将为您提供特色图片。否则使用

   get_the_post_thumbnail ( $new_reseller_id )

结束

相关推荐

Resize uploaded images

Possible Duplicate:Resizing all images 我有一个我建立的新闻门户,客户想要不同大小的特色图片。我已经准备好了我想要的第一个尺寸,他们已经发布了大约200多篇帖子,都准备好了这个尺寸。现在,如果我改变大小,它只会在新帖子上改变/或重新上传当前的特色图片(手工操作太多了)。我的问题是,有没有办法调整上传图像的大小?