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;
- }
- }
■ 打開HotNewspro/includes目錄中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;
- }
如果要顯示全部標題可以不加。
通過上面的函數替換,顯示的熱門排行可能更為合理一些。