不知道出于什么原因,1.30后的WP-PostViews插件取消了原先存在的get_timespan_most_viewed函數,這也是最近在網上得知的,因為最近有心情修改主題、插件,博客上面這個插件提供的小工具在邊欄的顯示內容幾個月都不怎么變,原因很簡單,設置的是瀏覽最多的文章,自然多的越來越多了,于是想修改下顯示設置,很遺憾后臺沒有相關設置,雖然有最近的,但是好像還不是我要的結果。
百度一陣子才知道原先這個插件有這個功能的,現在沒有了,但是有人在網上共享了之前的函數,這樣就簡單多了。把以下函數復制到wp-postviews.php文件的任意地方(當然是要合適點的任意地方,不要放其他函數中間就ok了)。
functionget_timespan_most_viewed($mode='',$limit=20,$days=1,$display=true){
global$wpdb,$post;
$limit_date=current_time('timestamp')-($days*86400);
$limit_date=date("Y-m-dH:i:s",$limit_date);
$where='';
$temp='';
if(!empty($mode)&&$mode!='both'){
$where="post_type='$mode'";
}else{
$where='1=1';
}
$most_viewed=$wpdb->get_results("SELECTDISTINCT$wpdb->posts.*,(meta_value+0)ASviewsFROM$wpdb->postsLEFTJOIN$wpdb->postmetaON$wpdb->postmeta.post_id=$wpdb->posts.IDWHEREpost_date<'".current_time('mysql')."'ANDpost_date>'".$limit_date."'AND$whereANDpost_status='publish'ANDmeta_key='views'ANDpost_password=''ORDERBYviewsDESCLIMIT$limit");
if($most_viewed){
foreach($most_viewedas$post){
$post_title=get_the_title();
$post_views=intval($post->views);
$post_views=number_format($post_views);
$temp.="<li><ahref=/"".get_permalink()."/">$post_title</a>-$post_views".__('views','wp-postviews')."</li>";
}
}else{
$temp='<li>'.__('N/A','wp-postviews').'</li>'."/n";
}
if($display){
echo$temp;
}else{
return$temp;
}
}
單單的放上以上語句其實還是不行的,雖然很多博客說可以,可能都是認為寫博客的都懂語言吧?雖然我也能改,但小子覺得這個對于那些不懂php為何物的新手來說,真的是難上加難啊。
這里小子為了大家方便就整理了下,為大家分享。
case'least_viewed':
get_least_viewed($mode,$limit,$chars);
break;
很簡單,打開wp-postviews.php文件在###Function:DisplayLeastViewedPage/Post或者其他的函數上面貼上以上函數,再次說明,函數添加位置隨意,但是盡量與其他業務函數放到一起,便于分析嘛!之后的操作就是再小工具的選項StatisticsType中添加get_timespan_most_viewed選項,這個直接在wp-postviews.php文件中搜索StatisticsType,你可以看到
<optionvalue="least_viewed"<?phpselected('least_viewed',$type);?>><?php_e('LeastViewed','wp-postviews');?></option>
或者其他選項的內容,復制一行,粘貼到上面或者下面(當然也可以是中間),然后將以上語句中least_viewed或者其他的你復制的語句中的值,修改為get_timespan_most_viewed,這樣就完成了后臺操作,不過目前選擇這個選項后邊欄是無顯示的,因為輸出的地方還要改進,下一步同樣是在wp-postviews.php文件中操作,搜索classWP_Widget_PostViews,在functionwidget函數中找到switch語句,其中應該有四個轉向,形如
case'least_viewed':
get_least_viewed($mode,$limit,$chars);
break;
不用想,重新復制一份,粘貼到并列的位置,將least_viewed修改為get_timespan_most_viewed,這樣,回到首頁刷新下,顯示效果應該是一天熱門的,因為是函數默認的(原函數是默認30天,小子設置為1天了),這里沒有在后臺添加選項,有需要可以自己需求修改函數體內數據即可。
另外,還有一點就是,上面提供的get_timespan_most_viewed函數是舊版本中的函數,不支持目前新版的輸出格式化等,這里小子又根據新的變量定義整理了如下函數,需要的可以直接復制替換上面提供的get_timespan_most_viewed函數來使用,函數體如下
$where="post_type='$mode'";
}else{
$where='1=1';
}
$most_viewed=$wpdb->get_results("SELECTDISTINCT$wpdb->posts.*,(meta_value+0)ASviewsFROM$wpdb->postsLEFTJOIN$wpdb->postmetaON$wpdb->postmeta.post_id=$wpdb->posts.IDWHEREpost_date<'".current_time('mysql')."'ANDpost_date>'".$limit_date."'AND$whereANDpost_status='publish'ANDmeta_key='views'ANDpost_password=''ORDERBYviewsDESCLIMIT$limit");
if($most_viewed){
foreach($most_viewedas$post){
$post_views=intval($post->views);
$post_title=get_the_title($post);
if($chars>0){
$post_title=snippet_text($post_title,$chars);
}
$post_excerpt=views_post_excerpt($post->post_excerpt,$post->post_content,$post->post_password,$chars);
$temp=stripslashes($views_options['most_viewed_template']);
$temp=str_replace("%VIEW_COUNT%",number_format_i18n($post_views),$temp);
$temp=str_replace("%POST_TITLE%",$post_title,$temp);
$temp=str_replace("%POST_EXCERPT%",$post_excerpt,$temp);
$temp=str_replace("%POST_CONTENT%",$post->post_content,$temp);
$temp=str_replace("%POST_URL%",get_permalink($post),$temp);
$output.=$temp;
}
}else{
$output='<li>'.__('N/A','wp-postviews').'</li>'."/n";
}
if($display){
echo$output;
}else{
return$output;
}
}
}
如函數參數所示,設置天數的地方已經用注釋標注了,這里仍然不再提供面板設置的選項了,需要的朋友可以自己折騰,在調試的時候可能因為當日訪問量過低,設置1天不能實現預期的顯示效果,就設置回30天了,大家可以根據需要設置。
新聞熱點
疑難解答