使用自定义帖子类型插入图像

时间:2014-12-11 作者:user892134

处理表单时,我希望将值插入到自定义帖子类型中products.

以下是我到目前为止所做的

$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\' => \'products\'
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    $attachment_id = media_handle_upload( \'art_upload[$i]\', $pid );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

$i++;
}
我使用了media\\u handle\\u upload和帖子id,但这不起作用。

我想知道的是如何将这些$\\u POST值插入到自定义POST类型中products 与图像一起显示为product_image?

自定义帖子类型为woocommerceproducts.

非常感谢您的帮助。举个例子就好了。

1 个回复
SO网友:moraleida

您当前的代码应该正确地在产品帖子类型中创建帖子。

您缺少更新保存图像的post meta的位。假设meta\\u键为product_image, 然后,在成功时运行此操作应执行以下操作:

update_post_meta( $pid, \'product_image\', $attachment_id );
另一方面,别忘了跑步wp_generate_attachment_metadatawp_update_attachment_metadata 上传图像后。

结束

相关推荐