读者墙,是博客程序,比如zblogPHP这样的模板需要用到的功能,读者墙顾名思义就是来到网站的访客,但细说起来是评论过文章的访客,因为只有评论了才有记录存在,才能获取访客的名字、头像、评论数字等信息!
下面分享的是@鸟儿博客分享的读者墙代码:
在include.php里加入:function 模板ID_Theme_Get_Readers(){
global $zbp;
$b = mktime(0,0,0,date('m')-1,date('d'),date('Y'));
$e = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
$i=20;
$sql = $zbp->db->sql->Select(
$zbp->table['Comment'],
array('COUNT(comm_ID) AS cnt, comm_Name, comm_HomePage , comm_Email'),
array(
array('<>', 'comm_Name', '访客'),
array('=', 'comm_AuthorID', 0),
array('BETWEEN', 'comm_PostTime', $b, $e),
array('CUSTOM', '1=1 GROUP BY comm_HomePage')
),
array('comm_PostTime' => 'DESC'),
$i,
null
);
$array=$zbp->db->Query($sql);
$s ="\r\n";
foreach ($array as $comment) {
$s .= '
http://www.gravatar.com/avatar/'
.md5(strtolower($comment['comm_Email'])).'&r=X&s=36" alt="">
';$s .="\r\n";
}
$s .="\r\n";
return $s;
}
此时,可以直接调用,而调用代码为:{模板ID_Theme_Get_Readers()},但这样调用每次都要获取数据,对于优化和速度以及访问量大的博客来说就很有压力了,所以最好缓存一个文件比较靠谱!
比如在上方代码中倒数第二行之前加入:@file_put_contents($zbp->usersdir . 'theme/NBlue/include/duzheqiang.php', $str);
最终上方大段代码为:function 模板ID_Theme_Get_Readers(){
global $zbp;
$b = mktime(0,0,0,date('m')-1,date('d'),date('Y'));
$e = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
$i=20;
$sql = $zbp->db->sql->Select(
$zbp->table['Comment'],
array('COUNT(comm_ID) AS cnt, comm_Name, comm_HomePage , comm_Email'),
array(
array('<>', 'comm_Name', '访客'),
array('=', 'comm_AuthorID', 0),
array('BETWEEN', 'comm_PostTime', $b, $e),
array('CUSTOM', '1=1 GROUP BY comm_HomePage')
),
array('comm_PostTime' => 'DESC'),
$i,
null
);
$array=$zbp->db->Query($sql);
$s ="\r\n";
foreach ($array as $comment) {
$s .= '
';$s .="\r\n";
}
$s .="\r\n";
@file_put_contents($zbp->usersdir . 'theme/NBlue/include/duzheqiang.php', $str);
return $s;
}
此时就会生成一个duzheqiang.php文件,这时候直接调取这个文件即可:
调取方式:{module:duzheqiang}
OK! 此时在本站内的读者墙调用就没有问题了!
那么问题来了,如果是站外调用zblogPHP的读者墙信息呢?
外部php程序,可以使用:
PHPecho file_get_contents('http://www.domain.com/zb_users/theme/NBlue/include/abcdef.php');//也就是产生文件的实际路径
进行调用。
外部静态页面,可以通过js引用以下文件进行动态js调用。
PHP<?php
require '../../../zb_system/function/c_system_base.php';
require '../../../zb_system/function/c_system_admin.php';
$zbp->Load();
if (!$zbp->CheckPlugin('Nobird_CMS_2')) {$zbp->ShowError(48);die();}
//Nobird_CMS_2 是主题ID
$name = GetVars('name','GET');
$file=$zbp->host . 'zb_users/theme/Nobird_CMS_2/include/'.$name.'.php';
$strcontent=GetHttpContent($file);
echo 'document.write(\''.$strcontent.'\');';
?>
以上内容存为html2js.php,存放在主题文件夹下,然后在静态页面使用:
Markup
进行调用(此处假设生成的文件名为Nobird_CMS_2_rdtags.php)。