Code Tự động lấy hình trong bài làm Featured Images trong wp

Mở file functions.php lên (functions.php nằm trong thư mục chứa theme mặc định trong wp của bạn)

//Set anh tieu bai trong bai viet function auto_featured_thumb () { global $post ; $has_thumb = has_post_thumbnail ( $post -> ID ); if (! $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' , 'auto_featured_thumb' ); add_action ( 'save_post' , 'auto_featured_thumb' ); add_action ( 'draft_to_publish' , 'auto_featured_thumb' ); add_action ( 'new_to_publish' , 'auto_featured_thumb' ); add_action ( 'pending_to_publish' , 'auto_featured_thumb' ); add_action ( 'future_to_publish' , 'auto_featured_thumb' );