Derrière ce nom barbare se cache le code pour fixer automatiquement la première image de l’article en Image à la Une.
Très pratique, surtout utilisée conjointement avec Cache Remote Images.
Petit bonus, le else, qui permet de fixer une image à la une par défaut…Plutôt bien pratique !
Voici le code, à rentrer n’importe où (je vous confiance pour ne pas faire n’importe quoi) dans functions.php :
/*———————————————————————————–*/
/* Autoset de l’image à la une
/*———————————————————————————–*/
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);
}
} else {
set_post_thumbnail($post->ID, ’1829′);
}
}
} //end function
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’);