Typecho评论添加QQ头像及名称
自定义评论样式
官方教程,我是基于里面的改的,修改时注意备份。
获取QQ号
我把昵称输入框修改了下,改了type
属性为number
,添加min
属性为10001
,避免填写过低的号码。
<input type="number" name="author" id="author" class="text" min="10001" value="<?php $this->remember('author'); ?>" required placeholder="QQ"/>
功能代码
<?php
header("content-Type: text/html; charset=Utf-8");
//获取QQ昵称QQ头像
if ($_GET['qq']) {
$qq = $_GET['qq'];
//向接口发起请求获取json数据
$get_info = file_get_contents('https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins='.$qq);
//转换编码
$get_info = mb_convert_encoding($get_info, "UTF-8", "GBK");
//对获取的json数据进行截取并解析成数组
$name = json_decode(substr($get_info,17,-1),true);
if($name and $qq){
$txUrl = 'https://q.qlogo.cn/headimg_dl?dst_uin='.$qq.'&spec=100';
$arr = array(
'code' => 1,
'msg' => 'Success',
'txUrl' => $txUrl,
'name' => urlencode($name[$qq][6])
);
exit(stripslashes(urldecode(json_encode($arr))));
}else{
$arr = array(
'code' => -1,
'msg' => 'Error'
);
exit(stripslashes(urldecode(json_encode($arr))));
}
}
?>
具体代码
<?php function threadedComments($comments, $options) {
$qq = $comments->author;//因为前面我们设置了输入QQ号,所以这里的评论者名称就是填的QQ号
$get_info = file_get_contents('https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins='.$qq);//获取QQ信息
//转换编码
$get_info = mb_convert_encoding($get_info, "UTF-8", "GBK");
//对获取的json数据进行截取并解析成数组
$name = json_decode(substr($get_info,17,-1),true);
//判断是否为空
if($name and $qq){
$txUrl = 'https://q.qlogo.cn/headimg_dl?dst_uin='.$qq.'&spec=40';//QQ头像,可以直接用这个链接加QQ号即可,后面的spec参数为尺寸,后面会放上各个尺寸的参数
$arr = array(
'code' => 1,
'msg' => 'Success',
'txUrl' => $txUrl,
'name' => urlencode($name[$qq][6])
);//name为QQ昵称
}else{
$arr = array(
'code' => -1,
'msg' => 'Error'
);
}
$commentClass = '';
$author = 0;//用户判断是否为博主
if ($comments->authorId) {
if ($comments->authorId == $comments->ownerId) {
$commentClass .= ' comment-by-author';
$author = 1;//是赋值1
} else {
$commentClass .= ' comment-by-user';
}
}
$commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
?>
<li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
if ($comments->levels > 0) {
echo ' comment-child';
$comments->levelsAlt(' comment-level-odd', ' comment-level-even');
} else {
echo ' comment-parent';
}
$comments->alt(' comment-odd', ' comment-even');
echo $commentClass;
?>">
<div class="comment-info">
<div class="comment-author">
<img src="
<?php if(urldecode($arr['name'])): ?>//判断获取的QQ昵称是否为空,因为输入QQ号无法判断是否为错误的,只能判断昵称
<?php echo urldecode($arr['txUrl']); ?>//如果不为空则输出QQ头像
<?php else:?>
<?php Helper::options()->themeUrl('logo.jpg'); ?>//为空则输出自定义头像
<?php endif;?>
" width="40" height="40" />
</div>
<?php if($author): ?>//判断是否为博主
<?php $name_fn = $comments->author; ?>//是则输出博主昵称
<?php else:?>
<?php if(urldecode($arr['name'])): ?>//否则判断是否获取的QQ昵称为空
<?php $name_fn = urldecode($arr['name']); ?>//否就输出QQ昵称
<?php else:?>
<?php $name_fn = "匿名"; ?>//为空就输出自定义昵称
<?php endif;?>
<?php endif;?>
<div class="comment-meta">
<span class="fn">
<?php if($comments->url): ?>//判断是否填了网址,是就输出a标签
<a href="<?php echo $comments->url; ?>" rel="external nofollow">
<?php echo $name_fn; ?>
</a>
<?php else:?>//否则直接输出昵称
<?php echo $name_fn; ?>
<?php endif;?>
</span>
<span class="comment-data"><?php $comments->date('y`m`d'); ?></span>
</div>
<span class="comment-reply"><?php $comments->reply(); ?></span>
</div>
<div class="comment-cont">
<?php $comments->content(); ?>
</div>
<div id="<?php $comments->theId(); ?>"></div>
<?php if ($comments->children) { ?>
<div class="comment-children">
<?php $comments->threadedComments($options); ?>
</div>
<?php } ?>
</li>
<?php } ?>
QQ头像尺寸参数
s/spec | px |
---|---|
1 | 40*40 |
2 | 40*40 |
3 | 100*100 |
4 | 140*140 |
5 | 640*640 |
40 | 40*40 |
100 | 100*100 |

测试
学习了。
测试一下
测试一下头像
测试
测试一下