【问题标题】:wordpress image upload for custom post type自定义帖子类型的wordpress图片上传
【发布时间】:2015-02-10 04:26:18
【问题描述】:

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

这是我的代码。

表单html

<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 功能可以将图片上传到自定义帖子类型?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    media_handle_upload() 是您要查找的函数:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多