从前端表单插入附件

时间:2015-04-02 作者:worldwildwebdev

我创建了一个前端表单,但突然无法提交图像附件。

请参阅以下未完成此操作的文章:http://voodoopress.com/including-images-as-attachments-or-featured-image-in-post-from-front-end-form/http://goldenapplesdesign.com/2010/07/03/front-end-file-uploads-in-wordpress/

将图像添加到媒体库或仅附加到帖子并在此后可管理的最佳方式是什么?

1 个回复
最合适的回答,由SO网友:worldwildwebdev 整理而成

我找到了一个有效的解决方案。这将通过将上载的文件附加到帖子并在循环中显示来处理这些文件。仍然没有设置特征图像。

if ( $_FILES ) {
    foreach ( $_FILES as $file => $array ) {
        $newupload = insert_attachment( $file, $pid_buy );
    }
}

function insert_attachment( $file_handler, $post_id, $setthumb=\'false\' ) {

    if ( $_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK ) {
        __return_false();
    }
    require_once( ABSPATH . "wp-admin" . \'/includes/image.php\' );
    require_once( ABSPATH . "wp-admin" . \'/includes/file.php\' );
    require_once( ABSPATH . "wp-admin" . \'/includes/media.php\' );
    $attach_id = media_handle_upload( $file_handler, $post_id );
    return $attach_id;
}

function show_all_thumbs() { // displaying the attached thumbs
    global $post;
    $post = get_post( $post );
    /* image code */
    $images =& get_children( \'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=\' . $post->ID );
    if ( $images ){
        foreach ( $images as $imageID => $imagePost ){
            unset( $the_b_img );
            $the_b_img  = wp_get_attachment_image( $imageID, \'thumbnail\', false );
            $thumblist .= \'<a href="\'.get_attachment_link($imageID).\'">\'.$the_b_img.\'</a>\';
        }
    }
    return $thumblist;
}

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢