HotNews Pro 2.7版侧边TAB菜单中的本月排行及年度排行,是通过判断日志评论数多少调用显示,如果安装并已启用WP-PostViews统计插件,可以利用WP-PostViews浏览统计功能,调用显示浏览次数最多的日志。
其实这个调用热门文章的方法,在HotNews主题较早的版本中已采用,只不过当时用的是另一款统计插件WP-PostViews Plus。最近有童鞋问如何根据日志点击数调用热门文章,下面就以首页TAB菜单中的年度排行为例,教大家实现这一功能替换。
■  WP-PostViews插件默认调用浏览次数最多日志的函数为:
    - <?php if (function_exists('get_most_viewed')): ?>   
-    <?php get_most_viewed(); ?>   
- <?php endif; ?>  
 
参考:http://wordpress.org/extend/plugins/wp-postviews/faq/
无时间段限制,不是我们想要的,但最新版的WP-PostViews插件确实无显示某时间段内日志的功能,不过早期版本中有相关的函数,下面是从1.11版本中复制出来的相关代码,加到WP-PostViews插件WP-PostViews.php中:
    - ### Function: Get TimeSpan Most Viewed - Added by Paolo Tagliaferri (http:  
- function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {   
-     global $wpdb, $post;       
-     $limit_date = current_time('timestamp') - ($days*86400);    
-     $limit_date = date("Y-m-d H:i:s",$limit_date);     
-     $where = '';   
-     $temp = '';   
-     if(!empty($mode) && $mode != 'both') {   
-         $where = "post_type = '$mode'";   
-     } else {   
-         $where = '1=1';   
-     }   
-     $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");   
-     if($most_viewed) {   
-         foreach ($most_viewed as $post) {   
-             $post_title = get_the_title();   
-             $post_views = intval($post->views);   
-             $post_views = number_format($post_views);   
-             $temp .= "<li><a href="".get_permalink()."">$post_title</a>".__('', 'wp-postviews')."</li>";   
-         }   
-     } else {   
-         $temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."n";   
-     }   
-     if($display) {   
-         echo $temp;   
-     } else {   
-         return $temp;   
-     }   
- }  
 
■  打开HotNewsproincludes目录中tab_h.php(该切换菜单只显示在首页,主题中还有另一个tab.php显示在其它页,替换方法类似),查找
    - <?php simple_get_most_vieweds(); ?>
 
替换为:
    - <?php if (function_exists('get_most_viewed')): ?>   
- <?php get_timespan_most_viewed('post',10,100, true, true); ?>   
- <?php endif; ?>  
 
其中:
post:显示单篇日志,若留空则显示单篇日志+页面,若填page则只显示页面。
10:显示的日志数量。
100:只显示100天内的日志,可酌情更改。
true:显示日志,若改为false则不显示文章。
false:不显示搜寻引擎机器人的查询次数,若改为true则全部显示
补充:调用某分类热门文章:
    - <?php if (function_exists('get_most_viewed_category')): ?>    
- <?php get_most_viewed_category(3, 'post', 10); ?>    
- <?php endif; ?>  
 
其中:数字3为分类ID,数字10为日志篇数。
■  最后,还而要给显示的日志标题加上文字截断,以免由于文字较多时出现回行,影响页面美观,打开主题样式文件style.css,查找:
    - .tab_latest ul li {   
-     width:212px;   
-     line-height:30px;   
-     border-bottom:1px dashed  #ccc;   
-     margin-bottom:-3px;   
-     }  
 
替换为:
    - .tab_latest ul li {   
-     width:212px;   
-     line-height:30px;   
-     border-bottom:1px dashed  #ccc;   
-     margin-bottom:-3px;   
-     height:30px;   
-     overflow: hidden;   
-     }  
 
如果要显示全部标题可以不加。
通过上面的函数替换,显示的热门排行可能更为合理一些。