포스트의 첫번째 이미지를 특성이미지(featured)로 설정

가끔 특성이미지(Featured) 설정하는 것을 잊어버리실 때가 있으신가요? 처음 워드프레스에 포스팅을 하실 경우 특성이미지를 설정하는 것을 잊어버리고, 블로그 페이지에 이미지가 나타나지 않는 경우가 있습니다. 이런 경우 대부분 당황하시면서 어떻게 해야 할지 모를 경우가 있습니다.

이럴경우 아래 코드를 theme 폴더 안에 있는 functions.php 파일에 복사해서 넣으시면 됩니다.

function autoset_featured() {
          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');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');