之前配合wp-postviews插件,通过添加:
###Get TimeSpan Most Viewed
function get_timespan_most_viewed($mode = \'\', $limit = 10, $days = 7, $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 $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>\".__(\'\', \'wp-postviews\').\"</li>\";
}
} else {
$temp = \'<li>\'.__(\'N/A\', \'wp-postviews\').\'</li>\'.\"n\";
}
if($display) {
echo $temp;
} else {
return $temp;
}
}
调用显示某时间段内浏览最多日志的功能。
近日,有童鞋问这些浏览最多的文章可不可以加上缩略图,简单修改了一下上述代码:
//按时间获得最受欢迎文章(带缩略图)
function get_timespan_most_viewed($mode = \'\', $limit = 10, $days = 7, $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 $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);
$thumb = \"<img src=\"\".get_post_meta(get_the_id(), \'thumbnail\', true).\"\">\";
$temp .= \"<li><a href=\"\".get_permalink().\"\">$post_title,$thumb</a>\".__(\'\', \'wp-postviews\').\"</li>\";
}
} else {
$temp = \'<li>\'.__(\'N/A\', \'wp-postviews\').\'</li>\'.\"n\";
}
if($display) {
echo $temp;
} else {
return $temp;
}
}
其中:
get_post_meta(get_the_id(), \'thumbnail\', true)
显示添加自定义栏目:thumbnail 文章的缩略图,也可以修改为调用特色图像:
the_post_thumbnail(\'post_thumbnail\');
调用方法,在主题模板适当位置添加:
<ul>
<?php if (function_exists(\'get_most_viewed\')): ?>
<?php get_timespan_most_viewed(\'post\',8,60, true, true); ?>
<?php endif; ?>
</ul>
以上方法仅供参考。
© 版权声明
1. 资源都是经过站长或作者收集测试修改后发布分享。如若转载请在文内以超链形式注明狐狸库文章出处,谢谢合作!
2. 本站除原创内容,其余所有内容均收集自互联网,仅限用于学习和研究目的,本站不对其内容的合法性承担任何责任。如有版权内容,请通知我们或作者删除,其版权均归原作者所有,本站虽力求保存原有版权信息,但因众多资源经多次转载,已无法确定其真实来源,或已将原有信息丢失,所以敬请原作者谅解!
3. 本站用户所发布的一切资源内容不代表本站立场,并不代表本站赞同其观点和对其真实性负责,若您对本站所载资源作品版权归属存有异议,请留言附说明联系邮箱,我们将在第一时间予以处理 ,同时向您表示歉意!为尊重作者版权,请购买原版作品,支持您喜欢的作者,谢谢!
4. 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客如有发现请立即向站长举报;本站资源文件大多存储在云盘,如发现链接或图片失效,请联系作者或站长及时更新。
THE END
请登录后查看评论内容