近期浏览最多(热门)文章添加缩略图

之前写过《替换HotNews Pro主题热门文章调用函数》配合wp-postviews插件,通过添加:

  1. ###Get TimeSpan Most Viewed
  2. function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {
  3. global$wpdb, $post;
  4. $limit_date = current_time('timestamp') - ($days*86400);
  5. $limit_date = date("Y-m-d H:i:s",$limit_date);
  6. $where = '';
  7. $temp = '';
  8. if(!empty($mode) && $mode != 'both') {
  9. $where = "post_type = '$mode'";
  10. } else {
  11. $where = '1=1';
  12. }
  13. $most_viewed = $wpdb->get_results("SELECT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
  14. if($most_viewed) {
  15. foreach ($most_viewedas$post) {
  16. $post_title = get_the_title();
  17. $post_views = intval($post->views);
  18. $post_views = number_format($post_views);
  19. $temp .= "<li><a href=\"".get_permalink()."\">$post_title</a>".__('', 'wp-postviews')."</li>";
  20. }
  21. } else {
  22. $temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
  23. }
  24. if($display) {
  25. echo$temp;
  26. } else {
  27. return$temp;
  28. }
  29. }

调用显示某时间段内浏览最多日志的功能。

近日,有童鞋问这些浏览最多的文章可不可以加上缩略图,简单修改了一下上述代码:

  1. //按时间获得最受欢迎文章(带缩略图)
  2. function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {
  3. global$wpdb, $post;
  4. $limit_date = current_time('timestamp') - ($days*86400);
  5. $limit_date = date("Y-m-d H:i:s",$limit_date);
  6. $where = '';
  7. $temp = '';
  8. if(!empty($mode) && $mode != 'both') {
  9. $where = "post_type = '$mode'";
  10. } else {
  11. $where = '1=1';
  12. }
  13. $most_viewed = $wpdb->get_results("SELECT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
  14. if($most_viewed) {
  15. foreach ($most_viewedas$post) {
  16. $post_title = get_the_title();
  17. $post_views = intval($post->views);
  18. $post_views = number_format($post_views);
  19. $thumb = "<img src=\"".get_post_meta(get_the_id(), 'thumbnail', true)."\">";
  20. $temp .= "<li><a href=\"".get_permalink()."\">$post_title,$thumb</a>".__('', 'wp-postviews')."</li>";
  21. }
  22. } else {
  23. $temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
  24. }
  25. if($display) {
  26. echo$temp;
  27. } else {
  28. return$temp;
  29. }
  30. }

其中:

  1. get_post_meta(get_the_id(), 'thumbnail', true)

显示添加自定义栏目:thumbnail 文章的缩略图,也可以修改为调用特色图像:

  1. the_post_thumbnail('post_thumbnail');

调用方法,在主题模板适当位置添加:

  1. <ul>
  2. <?php if (function_exists('get_most_viewed')): ?>
  3. <?php get_timespan_most_viewed('post',8,60, true, true); ?>
  4. <?php endif; ?>
  5. </ul>

以上方法仅供参考。

米粒在线
  • 本文由 发表于 2013年6月27日20:54:09
  • 转载请务必保留本文链接:http://www.miliol.org/8380.html
网站建设

WordPress上传文件自动重命名

推荐两段在上传媒体文件时自动重命名的代码。        代码一,按时间重命名 上传文件时会以“年月日时分秒+千位毫秒整数”的格式重命名文件,如“20161023122221765.jpg” ...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: