WP-PostRatings 只显示评分结果方法
WP-PostRatings 插件使用 <?php if(function_exists('the_ratings')) { the_ratings(); } ?>
代码添加评分功能,但这是一个完整模块。如果希望只希望显示评分结果,限制点击评分操作,可以用 <?php echo the_ratings_results( get_the_ID() ); ?>
获取,多余文本在插件模板选项里删除。
另一个方法是直接从数据库获取评分数据输出,方便修改结构样式。在主题 functions.php
文件里添加下面代码。
function showRatings($id, $css_class = 'show-ratings') { global $wpdb; $path = plugins_url('wp-postratings/images/'.get_option('postratings_image')); $rating = $wpdb->get_var("SELECT AVG(rating_rating) AS rating FROM $wpdb->ratings WHERE rating_postid = $id"); $i = 0; $html = '<div class="'.$css_class.'">'; if (!empty($rating)) { while ($i < floor($rating)) { $html .= '<img src="'.$path.'/rating_on.gif" alt="" />'; $i++; } if (round($rating, 1) == ($i+0.5)) { $html .= '<img src="'.$path.'/rating_half.gif" alt="" />'; $i++; } } while ($i < 5) { $html .= '<img src="'.$path.'/rating_off.gif" alt="" />'; $i++; } $html .= '</div>'; return $html; }
然后在主题模板文件里用 <?php echo showRatings( get_the_ID() ); ?>
代码调用。