大家使用WordPress寫文章時可能自己不想手動設定特色圖片,所以今天就來教大家如何自動設定文章(不包括以前發表的文章)的第一張圖片為特色圖片
外掛(插件)版
教程
將下方代碼添加到「佈景函式庫(functions.php)」的最底部 ?> 之前,然後點擊「更新檔案」就行啦!  
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* 設定所有文章第一個為特色圖片開始(由AREFLY.COM製作) */ function autoset_featured_image(){ 	global $post; 	$already_has_thumb = has_post_thumbnail($post->ID); 	if (!$already_has_thumb){ 		$attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1"); 		if ($attached_image){ 			foreach ($attached_image as $attachment_id => $attachment) { 				set_post_thumbnail($post->ID, $attachment_id); 			} 		} 	} } add_action('the_post', 'autoset_featured_image'); add_action('save_post', 'autoset_featured_image'); add_action('draft_to_publish', 'autoset_featured_image'); add_action('new_to_publish', 'autoset_featured_image'); add_action('pending_to_publish', 'autoset_featured_image'); add_action('future_to_publish', 'autoset_featured_image'); /* 設定所有文章第一個為特色圖片結束(由AREFLY.COM製作) */ | 
