Так что у меня проблема с моей петлей foreach, которая работает только один раз.foreach Loop Running Once
У меня есть следующие данные для переменной $ file_url.
$file_url = "http://www.somedomain.com/12355/1.jpg,http://www.somedomain.com/12355/2.jpg,http://www.somedomain.com/12355/3.jpg,http://www.somedomain.com/12355/4.jpg";
Теперь мой код выглядит следующим образом:
function fetch_media($file_url,$vin,$cacheid) {
require_once(ABSPATH . 'wp-load.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
global $wpdb;
if(!$vin) {
$vin = $cacheid;
}
$vin = $vin . '/';
//directory to import to
$artDir = "wp-content/uploads/vehiclephotos/$vin";
//if the directory doesn't exist, create it
if(!file_exists(ABSPATH.$artDir)) {
mkdir(ABSPATH.$artDir);
}
$file_url = explode(",", $file_url);
$gallery_images = array();
foreach ($file_url as $url) {
//rename the file... alternatively, you could explode on "/" and keep the original file name
$filename = array_pop(explode("/", $url));
if (@fclose(@fopen($url, "r"))) { //make sure the file actually exists
copy($url, ABSPATH.$artDir.$filename);
$siteurl = get_option('siteurl');
$file_info = getimagesize(ABSPATH.$artDir.$filename);
//create an array of attachment data to insert into wp_posts table
$artdata = array();
$artdata = array(
'post_author' => 1,
'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' => sanitize_title_with_dashes(str_replace("_", "-", $filename)),
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql'),
'post_type' => 'attachment',
'guid' => $siteurl.'/'.$artDir.$filename,
'post_mime_type' => $file_info['mime'],
'post_excerpt' => '',
'post_content' => ''
);
$uploads = wp_upload_dir();
$save_path = $uploads['basedir'].'/vehiclephotos/'.$vin.$filename;
//insert the database record
$attach_id = wp_insert_attachment($artdata, $save_path);
//generate metadata and thumbnails
if ($attach_data = wp_generate_attachment_metadata($attach_id, $save_path)) {
wp_update_attachment_metadata($attach_id, $attach_data);
}
array_push($gallery_images,$attach_id);
}
}
return serialize($gallery_images);
}
Так что выход я получаю сериализованный массив, который является:
a:1:{i:0;i:103525;}
Что хорошо, но так как было 4 элемента массива, он должен был зацикливаться 4 раза и дал мне сериализованные данные с 4 файлами attachment_id. Весь другой код там работает отлично один раз, он загружает изображение из URL-адреса, он переименовывает его и создает все миниатюры размером с фотографию в порядке.
Любая идея, что я делаю неправильно?
'$ fire_url! = $ File_url' – wogsland
Hah, который был опечаткой при его написании, это должно было прочитать file_url. – capsloc
используйте 'print_r ($ file_url)' перед foreach и проверьте правильность выбора Array. –