typecho读者墙(非插件版本)
想给自己博客放个读者墙,然而并不想用插件……typecho的读者墙都用了缓存插件(因为gravatar头像被墙的关系),缓存插件有时候用起来反而让网站速度变慢了……
所以我就在网上搜有没有不用插件的方式来实现读者墙的方法。
首先在模板文件夹的functions.php里添加下面这段代码。
/*读者墙*/
function getFriendWall(){
$period = time() - 2592000; // 单位: 秒, 时间范围: 30天
$db = Typecho_Db::get();
$sql = $db->select('COUNT(author) AS cnt', 'author', 'url', 'mail')
->from('table.comments')
->where('created > ?', $period )
->where('status = ?', 'approved')
->where('type = ?', 'comment')
->where('authorId = ?', '0')
->where('mail != ?', 'minami.s@qq.com') //排除自己上墙
->group('author')
->order('cnt', Typecho_Db::SORT_DESC)
->limit('16'); //读取几位用户的信息
$result = $db->fetchAll($sql);
$mostactive = "";
$my_array=array('secure','cn','1','2','3'); //我自定义的随机一个头像服务器,减少同时往一个服务器 发起多次请求
if (count($result) > 0) {
foreach ($result as $value) {
$mostactive .= '<li class="wall"><a href="' . $value['url'] . '" title="' . $value['author'] . ' : ' . $value['cnt'] . '次留言" target="_blank" rel="nofollow">';
$mostactive .= '<img class="avatar" src="http://'.$my_array[rand(0,3)].'.gravatar.com/avatar/'.md5(strtolower($value['mail'])).'?s=80&d=&r=G" /></a></li>';
}
echo $mostactive;
}
}
然后在你想要显示读者墙的地方粘帖如下代码,注意这个代码不能直接在日志里面编辑,得放到模板文件里才行。譬如post.php或者page.php之类的。
<?php echo getFriendWall();?>
我自己的的情况,是我想在留言页面放上读者墙,然而却并不想其他页面里也有。
那我就自己新建一个page-avatar.php文件,然后把page.php里面的内容全部复制过来。
<?php $this->content('(Read More)'); ?>
<?php echo getfriendwall();?>
然后把调用读者墙的代码贴在这个位置,就会显示在日志正文的下方了。当然你想放哪里都行=w=
下一步就是去css编辑你喜欢的读者墙排版方式咯!
li.wall img {width:50px;height:auto;/*设置自己喜欢的样子*/}
li.wall img:hover {/*设置自己喜欢的样子*/}
li.wall {float:left; /*让读者墙横排显示,也可以选择删除*/}
上面这一步完成以后把代码上传到服务器以后,就可以在后台进入留言的编辑页面,在右侧自定义模板处选择自己刚才新建的模板就可以了。
完成!打开看看效果吧^^
代码引用自: Typecho代码版读者墙(不带头像缓存)