typecho 显示用户文章总数量

模板开发以及发布测试

版主: seita

回复
头像
jrotty
帖子: 428
注册时间: 2015年 11月 2日 19:30

typecho 显示用户文章总数量

帖子 jrotty »

转自https://qqdie.com/archives/typecho-author-allpostsnum.html

functions.php中加入如下函数

代码: 全选

function allpostnum($id){
$db = Typecho_Db::get();
$co=$db->select ('table.contents.authorId')->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post');
$postnum = count($db->fetchAll($co));
echo $postnum;
}

显示当前作者文章总数量调用代码如下(适用于post.php,author.php, 或者index.phpwhile 循环中等等)

代码: 全选

<?php allpostnum($this->author->uid); ?>

显示特定uid作者文章总数量调用代码如下 (适用于任意模板文件)

代码: 全选

<?php allpostnum(1); ?>

//数字1改成对应作者的uid即可

疑问
SQL好像有个COUNT语法直接可以返回符合结果的数量,但是我没写出来,不知道怎么回事,怎么写都报错!
头像
远.山
帖子: 66
注册时间: 2017年 2月 2日 19:48
来自: https://moefo.cn

Re: typecho 显示用户文章总数量

帖子 远.山 »

function allpostnum($id){
$db = Typecho_Db::get();
$co=$db->fetchRow($db->select(array('COUNT(authorId)'=>'rbq'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
$postnum = $co['rbq'];
echo $postnum;
}
头像
远.山
帖子: 66
注册时间: 2017年 2月 2日 19:48
来自: https://moefo.cn

Re: typecho 显示用户文章总数量

帖子 远.山 »

还有个以前写的评论等级

//评论等级

function lv($i){
$db=Typecho_Db::get();
$mail=$db->fetchAll($db->select(array('COUNT(cid)'=>'rbq'))->from('table.comments')->where('mail = ?', $i)->where('authorId = ?','0'));
return dengji($mail['rbq']);
}

function dengji($i){
if($i<1){
return '蘭';
}elseif ($i<5 && $i>0) {
return 'バラ';
}elseif ($i<10 && $i>=5) {
return '椿';
}elseif ($i<15 && $i>=10) {
return 'ケシ';
}elseif ($i<20 && $i>=15) {
return '槿';
}elseif ($i<25 && $i>=20) {
return '蓮';
}elseif ($i>=25) {
return '桜';
}
}

可自定义样式的分类名称,同理也可以改为输出标签.

/*获取分类名称*/ A为文章ID B为输出分类名称最大数量,
function getTag($a,$b){
$db = Typecho_Db::get();
$tag = $db->fetchAll($db->select('table.metas.name')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('type=?','category')
->where('cid=?',$a)
->limit($b));
if($tag){
foreach($tag as $val){
$i=$i.'<span>'.$val['name'].'</span>';
}
}
return $i;
}
头像
jrotty
帖子: 428
注册时间: 2015年 11月 2日 19:30

Re: typecho 显示用户文章总数量

帖子 jrotty »

远.山 写了:function allpostnum($id){
$db = Typecho_Db::get();
$co=$db->fetchRow($db->select(array('COUNT(authorId)'=>'rbq'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
$postnum = $co['rbq'];
echo $postnum;
}

感谢么么哒
回复