fbpx

100% 주문제작형 고퀄리티 워드프레스홈페이지 제작 전문_워드프레서

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

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

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

 
 
100% 주문제작형 고퀄리티 워드프레스홈페이지 제작 전문_워드프레서 : http://wordpresser.co.kr/

저희 홈페이지를 방문해 주셔서 감사합니다.
고객분들을 위해 할 수 있는게 무엇이 있을까 고민 끝에

[무엇이든 물어 보세요] 코너를 오픈했습니다.