특성 이미지 없을 경우 첫 번째 이미지를 자동으로 설정하는 방법

워드프레스에서 포스트를 작성할 때 특성이미지를 설정하지 않으면 목록에 텍스트만 노출됩니다.
특성 이미지를 설정하지 않았을 경우 포스트에 있는 첫번째 이미지를 자동으로 설정하는 방법입니다.
 
아래의 코드를 차일드 테마를 만들어 함수 파일( functions.php )에 추가하면 됩니다.
 

function auto_featured_image() {
global $post;
if (!has_post_thumbnail($post->ID)) {
$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);
}
}
}
}
// Use it temporary to generate all featured images; 모든 특성 이미지를 임시 생성하는 데 사용
add_action('the_post', 'auto_featured_image');
// Used for new posts, 새 포스트에 사용
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');
// 출처: gavick.com
We value your privacy

We use cookies to make this website work and, with your permission, to understand how it is used and to measure our advertising. Necessary cookies are always on. Analytics and marketing cookies stay off unless you choose “Accept all” or switch them on under “Customize”. You can change your choice at any time from “Cookie settings” at the bottom of the page. Read our Privacy Policy.

Strictly necessaryNeeded for the site to work: security, your language choice and remembering your cookie settings. These cannot be switched off.
Always active