2016-11-19 10 views
0

おはよう。私は、foreachループを使用して、YouTubeの各ビデオにWordpressの投稿をプログラムで挿入する関数を書いています。Wordpressを生成する画像はforeachループで表示され、複数の投稿で同じ画像が表示されます

投稿のサムネイルを挿入するまで、すごくうまくいっています。私は(下記)に自動的にサムネイルのアップロードおよび挿入を処理する関数を使用して、ポストに関連付けています:

function Generate_Featured_Image($image_url, $post_id) { 
    $upload_dir = wp_upload_dir(); 
    $image_data = file_get_contents($image_url); 
    $filename = basename($post_id.'-'.$image_url); 
    if (wp_mkdir_p($upload_dir['path']))  $file = $upload_dir['path'] . '/' . $filename; 
    else          $file = $upload_dir['basedir'] . '/' . $filename; 
    file_put_contents($file, $image_data); 

    $wp_filetype = wp_check_filetype($filename, null); 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title' => sanitize_file_name($filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    $attach_id = wp_insert_attachment($attachment, $file, $post_id); 

    require_once(ABSPATH . 'wp-admin/includes/image.php'); 
    $attach_data = wp_generate_attachment_metadata($attach_id, $file); 
    $res1 = wp_update_attachment_metadata($attach_id, $attach_data); 
    $res2 = set_post_thumbnail($post_id, $attach_id); 
} 

この機能は、作業を行いますが、いくつかの奇妙な理由のために、それが唯一の画像をアップロードループの最後の動画たとえば、5つの動画がある場合、5つの投稿が作成されます。それぞれに固有の情報が含まれていますが、投稿のサムネイルはすべて最後の動画(5番目)の画像になります。彼らの誰も自分のサムネイルを持っていません。

はここの記事を作成し、私の機能のスリム化versonです:

ここ
function createYouTubePost() { 
    ...some other code... 

    $JSON  = file_get_contents('https://www.googleapis.com/youtube/v3/search?order='.$api_order.'&part='.$api_part.'&channelId='.$channel_id.'&maxResults='.$max_results.'&key='.$api_key); 
    $json_data = json_decode($JSON, true); 

    foreach ($json_data['items'] as $data) { 
     $video_id = $data['id']['videoId']; 
     $video_title = $data['snippet']['title']; 
     $video_description = $data['snippet']['description']; 
     $video_thumb_url = $data['snippet']['thumbnails']['high']['url']; 
     $video_thumb_width = $data['snippet']['thumbnails']['high']['width']; 
     $video_thumb_height = $data['snippet']['thumbnails']['high']['height']; 
     $video_publish_date = $data['snippet']['publishedAt']; 

     $args = array(
      'post_title' => substr($video_title, 0, strrpos($video_title, '(')), 
      'post_content' => $video_description, 
      'post_status' => 'publish', 
      'post_type' => 'download', 
     ); 

     if (!if_download_exists(substr($video_title, 0, strrpos($video_title, '(')))) { 
      $new_post_id = wp_insert_post($args, true); 

      if ($new_post_id == 0) { 
       echo '<br>Could not create the post.'; 
       var_dump($new_post_id); 
      } 
      else { 
       Generate_Featured_Image($video_thumb_url, $new_post_id); 

       ...lots of code to update various post_meta fields... 

       echo '<br>New post created.<br>'; 
       var_dump($new_post_id); 
      } 
     } 
    } 
} 

あなたはメディアの添付ファイルを見ることができ、それらはすべて同じだ方法:

enter image description here

そして、作成された個々の投稿は次のとおりです。

enter image description here

ご覧のとおり、各画像はそれぞれの投稿に割り当てられていますが、画像は同じです。

私はそれぞれの写真のファイル名を一意のIDで設定しようとしているので、それらはすべて異なっていますが、それは助けになりませんでした。また、関数に渡している画像のURLがすべて異なっていることを確認しました。

Generate_Featured_Image()ループでGenerate_Featured_Image()を使用していて、固有の情報でそれを証明しているのであれば、なぜループ内の最後の画像しか使用しないのですか?

ありがとうございました!

答えて

0

私は別の解決策を検討しました。 Wordpressのmedia_sideload_image()機能は機能し、私の状況のた​​めのより直接的な解決策です。ここで

は、私は今のポストにサムネイルを割り当てるために使用している機能である:

function generateFeaturedImage($image_url, $post_id) { 
    // required libraries for media_sideload_image 
    require_once(ABSPATH . 'wp-admin/includes/file.php'); 
    require_once(ABSPATH . 'wp-admin/includes/media.php'); 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 

    // $post_id == the post you want the image to be attached to 
    // $video_thumb_url == the vimeo video's thumb url 
    // $description == optional description 

    // load the image 
    $result = media_sideload_image($image_url, $post_id); 

    // then find the last image added to the post attachments 
    $attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC')); 

    if (sizeof($attachments) > 0) { 
     // set image as the post thumbnail 
     set_post_thumbnail($post_id, $attachments[0]->ID); 
    } 
} 

は、ここで私が見つけスタック交換溶液にthe linkです。

関連する問題