如果希望图像的URL不同,则需要先将图像文件复制到用户特定的文件夹,然后为复制的图像创建新附件—i、 e.克隆图像的“post”数据。
因此,请替换此:
// Get the attachments (IDs) attached to the `property` CPT.
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );
// Copy the meta value (attachments) to the `v_editor` CPT.
$vid_pix = get_post_meta( $vidID, \'vid_pix\', false ); // test
//delete_post_meta( $vidID, \'vid_pix\' );
foreach ( $prop_att_ids as $att_id ) {
... including the other code here ...
}
$vid_pix2 = get_post_meta( $vidID, \'vid_pix\', false ); // test
//var_dump( $vid_pix, $vid_pix2, $url ); // test
使用此选项:
$path2 = $yourID . \'/\' . $vidID; // relative to wp-content/uploads
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );
foreach ( $prop_att_ids as $att_id ) {
// Check if we have a valid image/attachment.
if ( $att_id && $file = get_attached_file( $att_id ) ) {
$filename = basename( $file );
$file2 = $path . \'/\' . wp_unique_filename( $path, $filename );
// Copy the image file to $path.
if ( @copy( $file, $file2 ) ) {
// Copy the attachment (post) data.
$att = get_post( $att_id, ARRAY_A );
unset( $att[\'ID\'] );
$att_id2 = wp_insert_attachment( $att, $file2 );
// Then add the meta data `vid_pix`.
add_post_meta( $vidID, \'vid_pix\', $att_id2 );
// Copy the attachment\'s meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data[\'file\'] = $path2 . \'/\' . basename( $file2 );
$data[\'sizes\'] = [];
wp_update_attachment_metadata( $att_id2, $data );
}
}
}
请注意,由于我们正在创建一个新的附件帖子,我使用了
wp_unique_filename()
为要复制到的图像文件生成唯一的文件名
$path
.
更新如果要生成所有缩略图,请替换此部分:
// Copy the attachment\'s meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data[\'file\'] = $path2 . \'/\' . basename( $file2 );
$data[\'sizes\'] = [];
wp_update_attachment_metadata( $att_id2, $data );
使用此选项:
// Create the attachment\'s meta data. (with thumbnails)
$att2 = get_post( $att_id2 );
wp_maybe_generate_attachment_metadata( $att2 );
以下是我使用的完整代码:
<?php
// File: import-pics.php
require_once $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\';
global $current_user, $imic_options; // Use global
wp_get_current_user(); // Make sure global is set, if not set it.
$yourID = $current_user->ID;
$propertyID = $_POST[\'propertyID\'];
$vidID = $_POST[\'videoID\'];
$path = WP_CONTENT_DIR . \'/uploads/\' . $yourID . \'/\' . $vidID;
$path2 = $yourID . \'/\' . $vidID; // relative to wp-content/uploads
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );
$to_copy = count( $prop_att_ids ); // test
$copied = 0; // test
foreach ( $prop_att_ids as $att_id ) {
// Check if we have a valid image/attachment.
if ( $att_id && $file = get_attached_file( $att_id ) ) {
$filename = basename( $file );
$file2 = $path . \'/\' . wp_unique_filename( $path, $filename );
// Copy the image file to $path.
if ( @copy( $file, $file2 ) ) {
// Copy the attachment (post) data.
$att = get_post( $att_id, ARRAY_A );
unset( $att[\'ID\'] );
$att_id2 = wp_insert_attachment( $att, $file2 );
// Then add the meta data `vid_pix`.
add_post_meta( $vidID, \'vid_pix\', $att_id2 );
// Copy the attachment\'s meta data. (no thumbnails)
$data = wp_get_attachment_metadata( $att_id );
$data[\'file\'] = $path2 . \'/\' . basename( $file2 );
$data[\'sizes\'] = [];
wp_update_attachment_metadata( $att_id2, $data );
$copied++; // test
}
} else {
$to_copy--; // test
}
}
echo $copied . \' image(s) out of \' . $to_copy . \' copied.<br>\'; // test
var_dump( get_post_meta( $vidID, \'vid_pix\', false ) ); // test
?>
PS:我之前把
$to_copy--;
, 虽然那只是测试用的