jeudi 19 avril 2012

Comment Définir automatiquement l’image à la une dans WordPress?

Souvent, je me trouve oubliant de cliquer sur « l’image à la une » quand j’écris un article. Mais je sais que j’en ai besoin, car il fait tourner le monde »pourJapprend.Com.
Quelle est la solution? Définir automatiquement l’image en vedette.
Voici un extrait de code rapide, vous pouvez jeter dans votre fichier functions.php qui va permettre à votre thème d’automatiquement mettre  la première image disponible.Noter que  vous pouvez toujours aller sélectionner une autre image et la mettre qu’une image en vedette si vous voulez. Il vérifie pour voir si une image aussi existe et sinon il va mettre la première image disponible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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');
?>

0 التعليقات:

Enregistrer un commentaire