원격 서버에서 이미지를 가져 와서 사용자 정의 필드로 설정하려고합니다. 첨부 파일 ID 가져 오기 및 생성이 작동하지만 사용자 정의 필드가 엉망입니다. 스레드 문제 또는 뭔가 있는지 나는 모르겠다.원격 이미지를 Wordpress로 가져온 다음 사용자 정의 필드로 설정하십시오.
$sql = 'SELECT * FROM produto';
$retval = mysql_query($sql, $connection) ;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$file = $row['imagem'];
$filename = basename($file);
$upload_file = wp_upload_bits($filename, null, file_get_contents($file));
if (!$upload_file['error']) {
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $parent_post_id,
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $parent_post_id);
$imgs[$row['modelo']][]['imagem'] = $attachment_id;
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']);
wp_update_attachment_metadata($attachment_id, $attachment_data);
}
}
}
foreach ($imgs as $key => $value) {
$args = array(
'post_type' => 'produto',
'meta_key' => 'identifier1',
'meta_value' => $key);
// The Query
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
update_field('imagens', $value, get_the_ID());
}
}
/* Restore original Post Data */
wp_reset_postdata();
}
나는이 사용자 정의 필드에 20 itens를 설정하려고하면, 그것은 완벽하게 작동합니다 :
여기 내 코드입니다. 하지만이 루프를 250 개의 결과로 실행해야하는 경우에는 중단됩니다.
내가 뭘 잘못하고 있니?
감사합니다!
안녕하세요, 답장을 보내 주셔서 감사합니다. 나는 당신의 기능과 그것이 취하는 흐름을 얻었습니다. 그러나 MySQL 쿼리는 어디에 두어야합니까? 이 방법으로 js 객체에 이미지 배열이 있다고 가정하지만 db에서 가져와야합니다. –
그럼 데이터베이스에서 모든 이미지를 가져와야합니다 (예를 들어'$ wpdb-> get_results'로). wpdb를 사용하면 결과를 객체 또는 배열로 정렬 할 수 있으므로 배열을 선택한 다음 ['json_encode']로 json으로 '변환'할 수 있습니다 (http://php.net/manual/en/function.json-encode .php), 그 값을 지역화 함수에서 지역화 할 수 있습니다. 또는 숨겨진 div에 넣으십시오. 자바 스크립트에서 가져올 수 있습니다. (지역화를 원하지 않는다면 ...) –
내'imagens' 사용자 정의 필드를 업데이트하려고 할 때 문제가 발생한다는 것을 깨달았습니다. 원격 이미지 가져 오기 및 WordPress에 업로드 잘 작동하지만, 쿼리 사용자 정의 필드를 업데이 트하는 과정은 어느 시점에서 휴식. –