指定 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 ;
}
}
?>
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> ";
}
}
?>
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 主题添加个性化小工具2011年01月16日 -
Wordpress 非插件统计网站访问信息2010年04月10日 -
Wordpress 非插件统计网站信息2010年03月20日 -
Wordpress 之链接平移 JQuery 特效2010年02月19日
我来坐坐~~博主厉害~~以后有机会向你请教哈。
呵呵,客气客气,欢迎常来哈~
觉得把鼠标放在文章标题上,标题会移动,超酷!!
呵呵,这是jquery特效,以前我有写过教程哟
请问,你link里用了 什么插件……
还有 你的twitter 怎么还能 用??
……
谷歌“wordpress重定向链接”第一篇就是啦,看来你不是经常来这个博客呀。
多翻翻万戈以前的文章,或许有你想要的。
你说的是友链页面还是评论者链接?总之我都没用插件
twitter嘛可以看这篇:http://wange.im/output-latest-twitter-to-wordpress-without-plugin.html
密码:wange.im
改起来还是挺麻烦的呀。
大名鼎鼎的万戈原来就在这里,以前好像来过,但是没留言,愿意好像是误以为你这个博客也是专做SEO的了,铺天盖地的SEO总让我眼花,我就不管SEO。
呵呵,我的博客基本没有做过什么SEO哟,SEO小白一个
COOL 学习啦 博客很棒呐~! 很好的用户体验 感觉你的主机应该很棒!
谢谢,我用的是传说中衡天小张的主机,嘿嘿
这个功能我感觉跟让文章置顶多篇是一样的吧?
这不一样哦,不过你到是给了我启发,可以折腾出些什么新花样,呵呵
头一次来,打个招呼
呵呵,欢迎常来啦~
(*^__^*) 嘻嘻……路过……我做了个论坛,只有自己访问,于是做了个随机主题,当成是最新主题
高手呀,何时我才能学到你那个水平?
尽情地折腾吧,会有收获的,呵呵
这个感觉用不上,有个随机日志感觉就差不多拉
我要多留言然后上头像哦!
我经常更新的,可以常来看看呐
万戈也是不照顾我们这些小盆友的心情啊 你都这样火了 你还这样说 我们该怎么活啊
想个法子救救我们这些一潭死水的小盆友啊
我在看你的博客的时候 忽然在想自己为什么要写博客 而博客应该朝着哪里去发展?这始终是个大问题。因为我现在有点迷茫了。。。
这是博客发展的必经阶段,坚持住
我刚开始做wordpress模板,还望万哥多多指点。上面的代码看懂了,回去实践下
客气客气,经常来交流哈
显示最近一个月内的热评日志,这个真的很需要。。
是呀,否则前几个日志都是被水出来的
每次来都能学到不少,这技术活我得一步步的跟着来捣鼓
呵呵,欢迎你常来呀~
我吃了早餐,哈哈
好像评论数少了许多是?
恩,大不如从前了,唉。。。
我要去伐万水军群里吼句
喜欢点击最高那个
恩,我用的就是点击最高那个方法
用代码黏住访客,这个主意不错。
只是为了更好的用户体验
这是针对热评,有没有针对文章访问量评行的代码?
使用方法是一样的,方法一中就拿访问量来作例子的
最近气氛不行了
不知为什么,感觉整体气氛都不如去年这段时间
新老替换不错
早该更新热评文章了,这个措施来得及时
一直那几篇,我自己都看着厌了
挺不错嘛,看什么时候折腾个去
当文囧的时候可以玩玩,哈哈
这是绕着弯子的让我们水你啊!
来吧,最近我缺水
谢谢老万,终于把那俩日本妞儿赶下最热门文章了……
沙发又消失
小朋友今天居然有时间上网
正好要问你一下,那个SSH帐号不能用了?
上Q
真不巧,板凳都没了!
好吧,虽然感觉挺新鲜的,但是有背弃历史的嫌疑。再说最近的日志都在前两页么不是,很好找的。——并且那块地方对我来说就是看最新评论的。认真地盖一层楼,感谢国家
其实用TAB,我也是为了节约地方
原来不是沙发~~这个方法用在边侧栏最好~
同意
沙发!!!!!!!!!!!!!!
超人同学鸡冻了
时间相差很多
刚从学校晚自习回来!