我們在有一些網(wǎng)站可以看到網(wǎng)站文章的瀏覽次數(shù)與評論最多的文件排行了,但wordpress中沒有帶這個功能,我們可以進行一些修改達到我們的功能了,下面一起來看看吧.
這里就不介紹頁面的建設方法了,明凱博客里面有介紹,搜索一下就可以了.
一、函數(shù)代碼,代碼如下:
- <?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>"; //Vevb.com
- $num++;
- }
- $output .= "</ul><br />";
- echo $output;
- }
- }
- ?>
二、調(diào)用方法,代碼如下:
- <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樣式,代碼如下:
- .hot_views li{
- border-bottom: 1px dashed #DDD;
- }
我這里的樣式非常簡單,因為調(diào)用了其他元素的樣式.
新聞熱點
疑難解答
圖片精選