2014-04-02 22 views
0

我正在修改生成縮略圖的wordpress插件。 感謝這個網站,我有先進的漂亮,但我堅持一個問題。 插件從帖子中的第一張圖片生成縮略圖。我需要從第三個圖像生成的。選擇數組元素或刪除前兩個

[URL=http://example.com/viewer.php?file=3030128910131424.jpg]  [IMG]http://example.com/images/30301289149131424_thumb.jpg[/IMG][/URL] [URL=http://example.com/viewer.php?file=6331951274718152.jpg][IMG]http://example.com/images/7633127470118152_thumb.jpg[/IMG][/URL] 

原代碼

// Initialize variable used to store list of matched images as per provided regular expression 
$matches = array(); 

// Get all images from post's body 
preg_match_all('/\[img\]([^\[\]>]*)/i', $post[0]->post_content, $matches); 

if (count($matches)) { 
    foreach ($matches[0] as $key => $image) { 
     /** 
     * If the image is from wordpress's own media gallery, then it appends the thumbmail id to a css class. 
     * Look for this id in the IMG tag. 
     */ 
     preg_match('/wp-image-([\d]*)/i', $image, $thumb_id); 
     $thumb_id = $thumb_id[1]; 

     // If thumb id is not found, try to look for the image in DB. Thanks to "Erwin Vrolijk" for providing this code. 
     if (!$thumb_id) { 
      $image = substr($image, strpos($image, '"')+1); 
      $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE guid = '".$image."'"); 
      $thumb_id = $result[0]->ID; 
     } 

     // Ok. Still no id found. Some other way used to insert the image in post. Now we must fetch the image from URL and do the needful. 
     if (!$thumb_id) { 
      $thumb_id = apt_generate_post_thumb($matches, $key, $post[0]->post_content, $post_id); 
     } 

     // If we succeed in generating thumg, let's update post meta 
     if ($thumb_id) { 
      update_post_meta($post_id, '_thumbnail_id', $thumb_id); 
      break; 
     } 
    } 
} 
}// end apt_publish_post() 

/** 
    * Function to fetch the image from URL and generate the required thumbnails 
*/ 

function apt_generate_post_thumb($matches, $key, $post_content, $post_id) 
{ 
// Make sure to assign correct title to the image. Extract it from img tag 
$imageTitle = ''; 
preg_match_all('/<\s*img [^\>]*title\s*=\s*[\""\']?([^\""\'>]*)/i', $post_content, $matchesTitle); 

if (count($matchesTitle) && isset($matchesTitle[1])) { 
    $imageTitle = $matchesTitle[1][$key]; 
} 

// Get the URL now for further processing 

$imageUrl = $matches[1][$key]; 
$imageUrl = str_replace("_thumb", "", $imageUrl); 

我已經試過兩件事情

// Get the URL now for further processing 

$imageUrl = $matches[3][$key]; 
$imageUrl = str_replace("_thumb", "", $imageUrl); 

不生成縮略圖的內容。

現在我試圖刪除前兩個數組變量,然後獲取url。我嘗試過array_splice和array_slice,但沒有得到任何結果。 是否有想法從第三張圖像創建縮略圖,而不是第一張?謝謝!

回答

0

我建議explode($matches),比運行,這是30 1 2計數「第二」元素的procedure

+0

我試過0,2,3,4,5並且不生成縮略圖..只是用1,我不知道爲什麼 – user2515464