这个需要配合post-views 代码或者插件来使用。实现效果如题~
使用方法function get_timespan_most_viewed($days = 30)
<code>
if (!function_exists('get_timespan_most_viewed')) {
function get_timespan_most_viewed($mode = '', $limit = 10, $chars = 0, $days = 30, $display = true)
{
global $wpdb;
$limit_date = current_time('timestamp') - $days * 86400;
$limit_date = date('Y-m-d H:i:s', $limit_date);
//$views_options = get_option('views_options');
$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 < '" . 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>') . '';
}
if ($display) {
echo $output;
} else {
return $output;
}
}
}</code>
if (!function_exists('get_timespan_most_viewed')) {
function get_timespan_most_viewed($mode = '', $limit = 10, $chars = 0, $days = 30, $display = true)
{
global $wpdb;
$limit_date = current_time('timestamp') - $days * 86400;
$limit_date = date('Y-m-d H:i:s', $limit_date);
//$views_options = get_option('views_options');
$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 < '" . 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>') . '';
}
if ($display) {
echo $output;
} else {
return $output;
}
}
}</code>