最近弄了一个 author page(作者页面),觉得好像少了些什么,这个页面没有显示作者的文章数和评论数,如果显示的话就比较完美了,于是在百度上找关于文章数和评论数的调用方法,然后集众家之长进行了优化。
下面开始教程:
第一步
在主题目录下面新建一个 author.php
文件,将以下代码复制进去。
[alt type="warning"]该代码只适用于 joe 主题,其他主题请自行修改页头、页脚文件指向如:<?php $this->need('public/head.php'); ?>[/alt]
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
if (empty($this->uid)) {$str = $_SERVER["REQUEST_URI"];
if (preg_match('/\d+/', $str, $arr)) {$id = $arr[0];
if ($this->user->hasLogin() && $this->user->uid == $id) {$lock = 0;} else {$lock = 1;}
$info = userok($id);
$this->user->uid = $id;
$this->user->screenName = $info['screenName'];
$this->user->mail = $info['mail'];
$this->user->group = $info['group'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->need('public/head.php'); ?>
</head>
<body>
<?php $this->options->JCustomBodyStart() ?>
<section id="joe">
<!-- 头部 -->
<?php $this->need('public/header.php'); ?>
<!-- 主体 -->
<section class="container j-index j-author">
<section class="j-adaption">
<div class="col-md-12">
<!-- 创作中心 s -->
<header class="page-header">
<div style="text-align:center;padding:10px">
<?php $email = $this->user->mail;
$imgUrl = ParseAvatar($email, 1);
echo '<img src="' . $imgUrl . '" style="border-radius: 50px;" height="50" width="50">'; ?>
<div style="margin:10px;"><?php $this->user->screenName(); ?></div>
<div><span style="border-radius: 3px;color: var(--background);padding: 5px;background: var(--theme);font-size: 14px;"><?php dengji($comments->mail);?></span></div>
<div style="margin:10px;"><span style="border-radius: 3px;color: var(--background);padding: 5px;background: var(--theme);font-size: 14px;"> 文章数:<?php echo allpostnum($this->author->uid); ?></span>
<span style="border-radius: 3px;color: var(--background);padding: 5px;background: var(--theme);font-size: 14px;"> 评论量:<?php echo commentnum($this->author->uid); ?></span>
</div>
<div style="margin:10px;">
<svg t="1608204283958" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3189" width="1em" height="1em"><path d="M826.46929 339.76682s19.698076 58.094327-94.090811 133.586954c0 0 55.19461-291.571526-302.170491-473.353774 0 0 99.090323 228.177717-108.089445 377.063177 0 0-78.792305-102.390001-226.677863-117.488526 0 0 61.394004 95.290694 56.394492 188.081633s-80.292159 196.880773-38.896201 328.567913c34.19666 108.789376 116.588614 214.879016 274.373206 247.775803-21.597891-54.894639-32.396836-112.589005 5.399472-261.774436 0 0 1.399863 123.587931 226.177913 255.275071 216.978811-57.494385 447.656284-264.574163 207.579728-677.733815z" p-id="3190" fill="#7a7b7c"></path></svg>
<?php echo allviewnum($this->user->uid); ?>
</div>
</div>
</header>
</div>
<section class="main <?php $this->options->JListType() ?>">
<div class="row author-page">
<div class="col-md-12">
<!-- 创作中心 e -->
<section class="j-index-article article">
<!-- 列表 -->
<?php $this->need('component/index.list.php'); ?>
</section>
</div>
</section>
</section>
</section>
<!-- 尾部 -->
<?php $this->need('public/footer.php'); ?>
</section>
<!-- 配置文件 -->
<?php $this->need('public/config.php'); ?>
</body>
</html>
第二步
在functions.php
放入以下代码 (joe 主题请放在core/core.php
文件里面)
判断是否会员 id
// 会员页判断是否会员 id
function userok($id){$db = Typecho_Db::get();
$userinfo=$db->fetchRow($db->select()->from ('table.users')->where ('table.users.uid=?',$id));
return $userinfo;
}
输出作者人气
/** 输出作者人气 */
function allviewnum($id){$db = Typecho_Db::get();
$postnum=$db->fetchRow($db->select(array('Sum(views)'=>'allviewnum'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
$postnum = $postnum['allviewnum'];
if($postnum=='0')
{return '暂无访问';}
elseif ($postnum>=10000) {return '1 万 + 访问';}
elseif ($postnum<10000 && $postnum>5000) {return '5K+ 访问';}
else{return ''.$postnum.' °c 访问 ';}
}
当前作者文章数
function allpostnum($id){$db = Typecho_Db::get();
$postnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'allpostnum'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
$postnum = $postnum['allpostnum'];
if($postnum=='0')
{return '暂无文章';}
else{return '文章'.$postnum.'篇';}
}
当前作者评论数
function commentnum($id){$db = Typecho_Db::get();
$commentnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'commentnum'))->from ('table.comments')->where ('table.comments.authorId=?',$id)->where('table.comments.type=?', 'comment'));
$commentnum = $commentnum['commentnum'];
if($commentnum=='0')
{return '暂无评论';}
else{return '评论'.$commentnum.'条';}
}
评论者认证等级
function dengji($i){$db=Typecho_Db::get();
$mail=$db->fetchAll($db->select(array('COUNT(cid)'=>'rbq'))->from('table.comments')->where('mail = ?', $i)->where('authorId = ?','0'));
foreach ($mail as $sl){$rbq=$sl['rbq'];}
if($rbq<1){echo '博主';}elseif ($rbq<10 && $rbq>0) {echo '闲逛';}elseif ($rbq<20 && $rbq>=10) {echo '小坐';}elseif ($rbq<40 && $rbq>=20) {echo '常憩';}elseif ($rbq<80 && $rbq>=40) {echo '久住';}elseif ($rbq<100 && $rbq>=80) {echo '永驻';}elseif ($rbq>=100) {echo '成家';}
}
温馨提醒
单独引用评论等级 <?php dengji($comments->mail);?>
单独引用文章数量 <?php echo allpostnum($this->author->uid); ?>
单独引用评论数量 <?php echo commentnum($this->author->uid); ?>
单独引用作者人气 <?php echo allviewnum($this->user->uid); ?>
再次感谢
https://www.ruletree.club/archives/878/
https://www.dpaoz.com/57
https://geekscholar.net/archives/12/
没看懂。这些代码应该放在最下面还是最上面
45329458@qq.com 2021-09-10
最下面
老狮 2021-09-11
作者页怎么调用呢qaq
火喵 2021-06-04
感谢
火喵 2021-08-20
{!{data:image/webp;base64,UklGRowIAABXRUJQVlA4WAoAAAAQAAAAAQMAKwEAQUxQSJQGAAANsMf//zk30quasH7MltP1bmrb9l0mNQ61bdtKU1u3aSe1bWZ6mFXt6bF4PIzs+fNr+8P+9Tn8ImICcP3v+t/1v+t/1/+u/13/u/53/e/63/W/63/X/67/Xf+7/nf97/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