我是一只历经沧桑的老狮

typecho作者页面,显示文章数和评论数

最近弄了一个 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/

本站未注明转载的文章均为原创,转载请注明来源,谢谢!

添加新评论