首页 > CMS > Wordpress > 正文

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

2024-09-07 00:51:17
字体:大 中 小
来源:转载
供稿:网友

我们在有一些网站可以看到网站文章的浏览次数与评论最多的文件排行了,但wordpress中没有带这个功能,我们可以进行一些修改达到我们的功能了,下面一起来看看吧.

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

一、函数代码,代码如下:

  1. <?php 
  2. //文章排行 
  3. function most_viewed($time,$limit) { 
  4. global $wpdb, $post; 
  5. $output = "<ul class=\"hot_views\">"; 
  6. $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"); 
  7. if($most_viewed) { 
  8. $num=1; 
  9. foreach ($most_viewed as $post) { 
  10. $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>"; 
  11. $num++; 
  12. } 
  13. $output .= "<br />"; 
  14. echo $output; 
  15. } 
  16. } 
  17. //评论排行 
  18. function most_commmented($time,$limit) { 
  19. global $wpdb, $post; 
  20. $output = "<ul class=\"hot_views\">"; 
  21. $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"); 
  22. if($most_viewed) { 
  23. $num=1; 
  24. foreach ($most_viewed as $post) { 
  25. $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>"; //Vevb.com 
  26. $num++; 
  27. } 
  28. $output .= "</ul><br />"; 
  29. echo $output; 
  30. } 
  31. } 
  32. ?> 

二、调用方法,代码如下:

  1. <h2>本月浏览量排行</h2> 
  2. <?php most_viewed(30,10); ?> 
  3. <h2>本月评论量排行</h2> 
  4. <?php most_commmented(30,10); ?> 
  5. <h2>年度浏览量排行</h2> 
  6. <?php most_viewed(365,10); ?> 
  7. <h2>年度评论量排行</h2> 
  8. <?php most_commmented(365,10); ?> 

三、CSS样式,代码如下:

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表