指定 WordPress 显示一段时间内的文章

可能是我最近水文太多,也可能是我疏于互访,近日我的 WordPress 人气一落千丈,大不如从前。看着侧边栏的“点击最高”和“热评日志”中的文章,已经好久没有赶超上来的文章,翻来翻去就这么几篇,对我来说是一种侧面打击,而且无论从用户体验、用户粘度或者 SEO 的角度来说都并不太好,所以今天在“点击最高”和“热评日志”的函数上做了些小小的手脚,加上了时间范围,让这两个版块上显示近一个月内的文章,那样就会有不断的新鲜文章更替上来。

正好,有两种限制时间范围的方法,我就分别介绍一下啦。

方法一:在 WordPress 主题的相关函数中找到 where 条件句,在其中加上一句条件句:post_date > date_sub( NOW(), INTERVAL 1 MONTH ),这个具体什么意思我就不解释了,还是很好理解的嘛,顺便拿自己的“点击最高”的代码作个例子,仅作参考哟,因为需要插件配合,不是通用的。

<?php
function hot_posts($limit = 10, $cut_off = 0, $before='<li>', $after='</li>'){
global $wpdb;
$home = get_option('siteurl');
$lastXs = $wpdb->get_results("SELECT ".$wpdb->posts.".ID AS ID,".$wpdb->posts.".post_title AS TITLE ,".$wpdb->post_counter.".post_count AS ZAEHLER FROM ".$wpdb->posts.",".$wpdb->post_counter." where ".$wpdb->post_counter.".post_id = ".$wpdb->posts.".ID AND post_type = 'post' AND post_date > date_sub( NOW(), INTERVAL 1 MONTH ) AND (".$wpdb->posts.".post_status = 'static' OR ".$wpdb->posts.".post_status = 'publish') order by ".$wpdb->post_counter.".post_count DESC limit 0,".$limit);
foreach ($lastXs as $lastX) {
$nummer = $lastX->ID;
$title = $lastX->TITLE;
$title = wptexturize($title);
if ($cut_off>0){
$title_short = substr($title,0,$cut_off);
}else{
$title_short = $title;
}
$hotposts = $lastX->ZAEHLER;
echo $before . "<span class=\"hotposttitle\"><a href='".get_permalink($nummer)."' title='".$title."'>". $title_short . "</a></span><span class=\"hotcomment\"> (".$hotposts." 次点击)</span>" . $after ;
}
}
?>

方法二:在 WordPress 主题的相关函数中找到 where 条件句,在其中加上一句条件句:TO_DAYS( now( ) ) - TO_DAYS( post_date ) < 31,这31就是限制的时间范围天数,这里限制了31天内,也就是一个自然月内啦,效果和方法一是一样一样的。用我的“热评日志”的代码作个例子,这个代码可以通用,尽情享用吧~

<?php
function mostcommented($limit = 5) {
global $wpdb, $post, $tableposts, $tablecomments, $time_difference, $post;
$mostcommenteds = $wpdb->get_results("SELECT  $tableposts.ID as ID, post_title, post_name, COUNT($tablecomments.comment_post_ID) AS 'comment_total' FROM $tableposts LEFT JOIN $tablecomments ON $tableposts.ID = $tablecomments.comment_post_ID WHERE comment_approved = '1' AND TO_DAYS( now( ) ) - TO_DAYS( post_date ) < 31 AND post_status = 'publish' AND post_password = ''  AND post_type = 'post' GROUP BY $tablecomments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");
foreach ($mostcommenteds as $post) {
$post_id = (int) $post->post_id;
$post_title = htmlspecialchars(stripslashes($post->post_title));
$comment_total = (int) $post->comment_total;
$permalink = get_permalink($post->ID);
echo "<li><span class=\"hotposttitle\"><a title='$post_title' href=\"$permalink\">$post_title</a></span><span class=\"hotcomment\">($comment_total 条评论)</span></li> ";
}
}
?>

效果就见我的侧边栏 TAB 吧,现在显示的都是最近一个月内的点击最高和热评日志。

    • 谷歌“wordpress重定向链接”第一篇就是啦,看来你不是经常来这个博客呀。
      多翻翻万戈以前的文章,或许有你想要的。

    • 你说的是友链页面还是评论者链接?总之我都没用插件
      twitter嘛可以看这篇:http://wange.im/output-latest-twitter-to-wordpress-without-plugin.html
      密码:wange.im

  1. 改起来还是挺麻烦的呀。
    大名鼎鼎的万戈原来就在这里,以前好像来过,但是没留言,愿意好像是误以为你这个博客也是专做SEO的了,铺天盖地的SEO总让我眼花,我就不管SEO。

  2. 我在看你的博客的时候 忽然在想自己为什么要写博客 而博客应该朝着哪里去发展?这始终是个大问题。因为我现在有点迷茫了。。。

  3. 真不巧,板凳都没了! :sad: 好吧,虽然感觉挺新鲜的,但是有背弃历史的嫌疑。再说最近的日志都在前两页么不是,很好找的。——并且那块地方对我来说就是看最新评论的。认真地盖一层楼,感谢国家 :cool: