1.30后的WP-PostViews插件取消了原先存在的get_timespan_most_viewed函数,所以需要自己在wp-postviews.php代码里填写这个函数
function get_timespan_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
global $wpdb; $views_options = get_option('views_options'); $limit_date = date('Y-m-d H:i:s', strtotime('-30 days')); $where = ''; $temp = ''; $output = ''; 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>'".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit"); //print_r($most_viewed); 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; }}
打开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> <option value="get_timespan_most_viewed"<?php selected('get_timespan_most_viewed', $type); ?>><?php _e('Get_Timespan_Most_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;case 'get_timespan_most_viewed':
get_timespan_most_viewed($mode, $limit, $chars); break; 不用想,重新复制一份,粘贴到并列的位置,将least_viewed修改为get_timespan_most_viewed,这样,回到首页刷新下,显示效果应该是30天热门的,因为是函数默认的(原函数是默认30天),这里没有在后台添加选项,有需要可以自己需求修改函数体内数据即可。问题:粘贴代码后发现侧边栏小工具不显示数据,调试发现在数据库能查询出30天的排行数据但就是页面显示不出来,一行一行调试发现问题出现在$display这个变量上,函数里虽然设了默认为true,但在if语句里它的值是false,导致$output这个变量之前有值,但在if($display)判断后,值被return了,所以在页面上没显示出来,而导致$display为false的原因是在调用函数的时候我是这样写的 get_timespan_most_viewed($mode, $limit, $chars, $display);后来把最后的参数去掉,显示