我有循环遍历的数据,这些数据包括标题、内容、服务器上文件的路径以及各种其他信息。
目标是遍历数据并在循环中创建带有 post_meta product_image 的图片的 Worpress 帖子
如:add_post_meta ($ post_id, '_product_image', $ attach_id, true);
我的循环看起来像这样:
while($row = $STH->fetch()) {
$my_post = array(
'post_title' => $row->title,
'post_content' => $row->description,
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_category' => array(6)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
add_post_meta( $post_id, 'product_price', 199, true );
add_post_meta( $post_id, 'product_sub_price', 20, true);
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
$filename = $row->image_local_name;
// Path to the file i want to upload into wordpress.
$path = ABSPATH . 'add/foo/uploads/' . $row->image_local_name; // Path to file.
var_dump($path);
$file_url = $row->image_url;
$filename = $row->image_local_name;
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
// Path i want to save the image(s).
$save_path = $wp_upload_dir['basedir'] . $wp_upload_dir['subdir'] . $filename;
var_dump($save_path);
$attachment = array(
'post_author' => $current_user->ID,
'post_date' => current_time('mysql'),
'post_date_gmt' => current_time('mysql'),
'post_title' => $filename,
'post_status' => 'inherit',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_name' => $filename,
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql'),
'post_parent' => $post_id,
'post_type' => 'attachment',
'guid' => $wp_upload_dir['basedir'] . $filename, // not sure if this is correct, some advise?
'post_mime_type' => $wp_filetype['type'],
'post_excerpt' => '',
'post_content' => ''
);
$attach_id = wp_insert_attachment( $attachment, $save_path, $post_id );
var_dump($attach_id);
$attach_data = wp_generate_attachment_metadata( $attach_id, $path );
var_dump($attach_data);
$update = wp_update_attachment_metadata( $attach_id, $attach_data );
var_dump($update);
// Add the attach_id to post_meta key: product_image with the value of the attach_id
add_post_meta( $post_id, '_product_image', $attach_id, true);
}
输出:http://pastebin.com/LpNzc1pP
似乎它在数据库中正确存储,但是wordpress创建的图像,例如:racing cars-with-studs-including-free-shipping-mattress-amp ribb.jpg
图像保存在六个版本中,这是正确的,但它们没有保存在我希望放置这些图像的uploads/08 中。
图像存储在这里:
/Applications/MAMP/htdocs/websites/foo.dev/public_html/add/foo/uploads/
如何将这些图像保存在正确的位置?
最佳答案
虽然我没有解决您的具体问题,但我知道您正在尝试完成什么,并且我建议使用 Wordpress API 的替代方法 media_handle_sideload()
或 media_sideload_image()
功能将图像文件放入媒体库并自动附加到帖子中。
所以而不是:
$filename = $row->image_local_name;
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
// Path i want to save the image(s).
$save_path = $wp_upload_dir['basedir'] . $wp_upload_dir['subdir'] . $filename;
var_dump($save_path);
$attachment = array(
'post_author' => $current_user->ID,
'post_date' => current_time('mysql'),
'post_date_gmt' => current_time('mysql'),
'post_title' => $filename,
'post_status' => 'inherit',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_name' => $filename,
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql'),
'post_parent' => $post_id,
'post_type' => 'attachment',
'guid' => $wp_upload_dir['basedir'] . $filename, // not sure if this is correct, some advise?
'post_mime_type' => $wp_filetype['type'],
'post_excerpt' => '',
'post_content' => ''
);
$attach_id = wp_insert_attachment( $attachment, $save_path, $post_id );
var_dump($attach_id);
$attach_data = wp_generate_attachment_metadata( $attach_id, $path );
var_dump($attach_data);
$update = wp_update_attachment_metadata( $attach_id, $attach_data );
var_dump($update);
// Add the attach_id to post_meta key: product_image with the value of the attach_id
add_post_meta( $post_id, '_product_image', $attach_id, true);
你可以使用:
$myAttachmentId = media_handle_sideload( $file_url, $post_id );
这会将图片作为帖子的附件上传到媒体库,然后您可以通过以下方式将其设为“产品图片”:
$myProductImage = add_post_meta( $post_id, '_product_image', $myAttachmentId, true );
关于wordpress - 如何以编程方式上传到 wordpress - 保存文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18276169/