Miniature wp-postratings
Par zorrito le 2 juin 2009 Ă 10:39 :: codage
L’excellent plugin wordpress wp-postratings permet de pouvoir voter pour les articles de son blog.
On peux avec ce dernier afficher un top des meilleurs articles par exemple.
Cependant on ne peux pas y faire figurer une miniature de son article (on suppose que pour la gestion des miniatures de vos articles vous utilisez un champs personnalisĂ© qui contiendra l’url de votre miniature)
Nous allons voir ensemble comment faire cela.
Dans notre cas, nous allons modifier le script pour cette fonction ### Function: Display Highest Rated Page/Post
Ouvrez le fichier postratings-stats.php et allez Ă la ligne 217.
Nous allons trouver la requĂȘte qui nous permet d’aller rĂ©cupĂ©rer les informations dans notre base de donnĂ©e, nous allons la modifier comme ci dessous.
$highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score , (t4.meta_value) AS image FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID LEFT JOIN wp_postmeta AS t4 ON t4.post_id = wp_posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND t4.meta_key = 'image' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
Avec cette requĂȘte nous allons donc pouvoir rĂ©cupĂ©rer l’url de notre miniature qui est contenu dans un champs personnalisĂ© ‘image’.
Vous pouvez l’adapter Ă votre guise pour rĂ©cupĂ©rer d’autres champs personnalisĂ©s par exemple.
Maintenant nous allons ouvrir le fichier wp-postratings.php et nous allons modifier la fonction function expand_ratings_template qui se trouve Ă partir de la ligne 934.
Par défaut nous avons ceci.
// Get post related variables
if(is_null($post_ratings_data)) {
$post_ratings_data = get_post_custom($post_id);
$post_ratings_users = intval($post_ratings_data['ratings_users'][0]);
$post_ratings_score = intval($post_ratings_data['ratings_score'][0]);
$post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);
} else {
$post_ratings_users = intval($post_ratings_data->ratings_users);
$post_ratings_score = intval($post_ratings_data->ratings_score);
$post_ratings_average = floatval($post_ratings_data->ratings_average);
}
Nous allons le transformer comme ceci.
// Get post related variables
if(is_null($post_ratings_data)) {
$post_ratings_data = get_post_custom($post_id);
$post_ratings_users = intval($post_ratings_data['ratings_users'][0]);
$post_ratings_score = intval($post_ratings_data['ratings_score'][0]);
$post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);
//MODIF ICI
$img = $post_ratings_data['image'][0];
} else {
$post_ratings_users = intval($post_ratings_data->ratings_users);
$post_ratings_score = intval($post_ratings_data->ratings_score);
$post_ratings_average = floatval($post_ratings_data->ratings_average);
//MODIF ICI
$img = $post_ratings_data->image;
}
Ensuite dirigez vous vers les lignes 1000 ou il y a une succession de str_replace.
Nous allons rajouter ceci.
$value = str_replace("%THUMB%", $img, $value);
Le tour est joué.
Dans les options de template de wp-postratings, vous allez pouvoir utiliser %THUMB% pour afficher une miniature de votre article.
<a href="%POST_URL%" title="%POST_TITLE%"><img class="top-articles" src="http://localhost/wordpress/wp-content/themes/votre theme/thumb.php?src=%THUMB%" alt="%POST_TITLE%" width="91" height="67"/></a>
Le principe gĂ©nĂ©ral est exposĂ©, j’espĂšre que cela vous aura Ă©tĂ© utile.
1 commentaire »

Fonction trĂšs intĂ©ressante, mais ne semble pas fonctionner avec le version 1.5 du plug-in. Ca pourrais ĂȘtre aussi intĂ©ressant de voir comment il est possible d’intĂ©grĂ© les rĂ©sultats sous forme de vignette comme par ici : http://www.fubiz.com