wordpress添加阅读和评论排行榜功能例子
我们在有一些网站可以看到网站文章的浏览次数与评论最多的文件排行了,但wordpress中没有带这个功能,我们可以进行一些修改达到我们的功能了,下面一起来看看吧.
这里就不介绍页面的建设方法了,明凯博客里面有介绍,搜索一下就可以了.
一、函数代码,代码如下:
- <?php
-
- function most_viewed($time,$limit) {
- global $wpdb, $post;
- $output = "<ul class=\"hot_views\">";
- $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS post_views_count FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date > date_sub( now(), interval $time day ) AND post_type ='post' AND post_status = 'publish' AND meta_key = 'post_views_count' AND post_password = '' ORDER BY post_views_count DESC LIMIT $limit");
- if($most_viewed) {
- $num=1;
- foreach ($most_viewed as $post) {
- $output .= "\n<li><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title." (".$post->post_views_count."+)\" >$num. ". $post->post_title." (".$post->post_views_count."+)</a></li>";
- $num++;
- }
- $output .= "<br />";
- echo $output;
- }
- }
-
- function most_commmented($time,$limit) {
- global $wpdb, $post;
- $output = "<ul class=\"hot_views\">";
- $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.* FROM $wpdb->posts WHERE post_date > date_sub( now(), interval $time day ) AND post_type ='post' AND post_status = 'publish' AND post_password = '' ORDER BY comment_count DESC LIMIT $limit");
- if($most_viewed) {
- $num=1;
- foreach ($most_viewed as $post) {
- $output .= "\n<li><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title." (".$post->comment_count."+)\" >$num. ". $post->post_title." (".$post->comment_count."+)</a></li>";
- $num++;
- }
- $output .= "</ul><br />";
- echo $output;
- }
- }
- ?>
二、调用方法,代码如下:
- <h2>本月浏览量排行</h2>
- <?php most_viewed(30,10); ?>
- <h2>本月评论量排行</h2>
- <?php most_commmented(30,10); ?>
- <h2>年度浏览量排行</h2>
- <?php most_viewed(365,10); ?>
- <h2>年度评论量排行</h2>
- <?php most_commmented(365,10); ?>
三、CSS样式,代码如下:
- .hot_views li{
- border-bottom: 1px dashed #DDD;
- }
我这里的样式非常简单,因为调用了其他元素的样式.