自定义帖子类型的WordPress图像上传


wordpress image upload for custom post type

我正在为自定义帖子类型创建图像上传表单,但我不知道如何将图像上传到自定义帖子类型。 自定义帖子类型称为"艺术"。

这是我的代码。

表单网页

<form id="art_submission_form" enctype="multipart/form-data" method="post" action="art_process.php">
     <div class="art_upload_container" <?php echo $style; ?>>
            <div class="art_sub_row">
            <select class="art_sub_select" name="art_creation_date">
                <option value="Creation Date">Creation Date</option>
            <?
                $dates = array("2014","2013","2012","2011","2010","2009","2008","2007","2006","2005","2004","2003","2002","2001","2000","1999",
                "1998","1997","1996","1995","1994","1993","1992","1991","1990","1989","1988","1987");
                foreach($dates as $value) {
                    echo "<option value='$value'>$value</option>";
                }
            ?>
            </select>
            <div class="art_sub_col"><label>Art Title</label><input type="text" class="art_title" name="art_title[]" value="<?php echo $art_title; ?>" /></div>
            </div>
            <div class="art_sub_row">
            <div class="art_sub_col"><label>Width(cm)</label><input type="text" class="art_width" name="art_width[]" value="<?php echo $art_width; ?>" /></div>
            <div class="art_sub_col"><label>Height(cm)</label><input type="text" class="art_height" name="art_height[]" value="<?php echo $art_height; ?>" /></div>
            <div class="art_sub_col"><label>Price</label><input type="text" class="art_price" name="art_price[]" value="<?php echo $art_price; ?>" /></div>
            </div>
                <div class="art_sub_row">
            <div class="art_sub_col"><label>Upload Art:</label> <input type="file" name="art_upload[]" class="art_upload"></div>
            </div>
        </div>
<input type="submit" class="art_sub_send" value="Send" />
</form>

art_process.php - 到目前为止我做了什么

  $art_title = $_POST['art_title'];
    $i = 0;
    foreach($art_title as $value) {
    $art_title = $_POST['art_title'][$i];
    $art_width = $_POST['art_width'][$i];
    $art_height = $_POST['art_height'][$i];
    $art_price = $_POST['art_price'][$i];
    $art_creation_date = $_POST['art_creation_date'][$i];

  // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $art_title,
        'post_status'   => 'draft',    
        'post_type' => 'art'
    );
    //save the new post
    $pid = wp_insert_post($new_post); 

    $i++;
    }

是否有内置的wordpress功能可以将图像上传到自定义帖子类型?

media_handle_upload()

您要查找的函数:

处理文件上传 POST 请求,并创建附件帖子 在数据库中。

如果成功,您可以使用父帖子 ID ( $pid ) 和附件 ID 将图像设置为带有 set_post_thumbnail() 的帖子缩略图。