明凯博客

关注网站技术,一个特立独行的程序员

wordpress添加阅读和评论排行榜功能

上次写了一篇文章用来介绍排行榜的。

WordPress 文章浏览次数与点击排行榜

很多读者问为什么和我博客的排行榜不一样呢。

http://www.aimks.com/hot

其实我博客的排行榜是加了评论排行榜的,新建的一个页面。

这里就不介绍页面的建设方法了。明凯博客里面有介绍。搜索一下就可以了。

一、函数代码:

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
< ?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;
}
}
?>

二、调用方法:

1
2
3
4
5
6
7
8
<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样式:

1
2
3
.hot_views li{
border-bottom: 1px dashed #DDD;
}

我这里的样式非常简单。因为调用了其他元素的样式。

下面提供下载地址:

http://pan.baidu.com/s/1c07w20g

, ,

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注