不知道出于什么原因,1.30后的WP-PostViews插件取消了原先存在的get_timespan_most_viewed函数,这也是最近在网上得知的,因为最近有心情修改主题、插件,发现小子的博客上面这个插件提供的小东西在边栏的显示内容几个月都不怎么变,原因很简单,设置的是阅读最多的文章,自然多的越来越多了,于是想修改下显示设置,很遗憾后台没有相关设置,虽然有最近的,可是仿佛还不是我要的成果。
谷歌一阵子才知道原先这个插件有这个功能的,现在没有了,可是有人在网上同享了之前的函数,这样就简单多了。把以下函数复制到wp-postviews.php文件的任意处所(当然是要适合点的任意处所,不要放其他函数中间就ok了)。
function get_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-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 DISTINCT $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> - $post_views ".__('views', 'wp-postviews')."</li>";}} else {$temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."n";}if($display) {echo $temp;} else {return $temp;}}
单单的放上以上语句其实仍是不可的,虽然很多博客说可以,可能都是认为写博客的都懂语言吧?虽然我也能改,但小子感觉这个对那些不懂php为何物的新手来讲,真的是难上加难啊。
这里小子为了年夜家便利就整理了下,为年夜家分享。
很简单,打开wp-postviews.php文件在### Function: Display Least Viewed Page/Post或其他的函数上面贴上以上函数,再次说明,函数添加位置随意,可是尽可能与其他业务函数放到一起,便于阐发嘛!之后的操作就是再小东西的选项Statistics Type中添加get_timespan_most_viewed选项,这个直接在wp-postviews.php文件中搜索Statistics Type,你可以看到
<option value="least_viewed"<?php selected('least_viewed', $type); ?>>< ?php _e('Least Viewed', 'wp-postviews'); ?></option>
或其他选项的内容,复制一行,粘贴到上面或下面(当然也可以是中间),然后将以上语句中least_viewed或其他的你复制的语句中的值,修改成get_timespan_most_viewed,这样就完成了后台操作,不过目前选择这个选项后边栏是无显示的,因为输出的处所还要改进,下一步同样是在wp-postviews.php文件中操作,搜索class WP_Widget_PostViews,在function widget函数中找到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("SELECT DISTINCT $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_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;}}}