实现每评论一次自动排第一功能(Typecho版)

折腾博客

温馨提示:这篇文章已超过1173天没有更新,请注意相关的内容是否还可用!

下面是主要代码(插入到 functions.php 中):

function Autofirst(){
    $db = Typecho_Db::get();
    $query = $db->select()->from('table.comments')->where('authorId = ?','0')->order('coid',Typecho_Db::SORT_DESC)->limit(100);
    $result = $db->fetchAll($query);
    $arrUrl = array();
    $arrAuthor = array();
    foreach ($result as $value) {
        if($value["url"]!==null){
            array_push($arrUrl,$value["url"]);
            array_push($arrAuthor,$value["author"]);
        }
    }
    $su=array_filter(array_merge(array_unique($arrUrl)));
    $sa=array_filter(array_merge(array_unique($arrAuthor)));
    $num=0;
    for($i=0;$i<count(array_unique($su));$i++){
        if($su[$i]!=="" && $num<16){
            $num+=1;
            $db1 = Typecho_Db::get();
            $query1 = $db1->select()->from('table.comments')->where('url = ?',$su[$i])->order('coid',Typecho_Db::SORT_DESC)->limit(100);
            $result1 = $db1->fetchAll($query1);
            $arrAuthor1 = array();
            foreach ($result1 as $value) {
                    array_push($arrAuthor1,$value["author"]);
            }
            echo '<a href="'.$su[$i].'" rel="external nofollow" class="item col-lg-3 col-md-3 visible-lg visible-md" target="_blank"><i class="glyphicon glyphicon-leaf"></i> '.$arrAuthor1[0].'</a>';
        }
    }
}

调用代码和 css:

<div class="row autofirst visible-lg visible-md">
<p>
<i class="glyphicon glyphicon-th"></i> 每评论一次就会自动排在本栏目第一位
</p>
    <?php Autofirst(100) ?>
</div>
<style>
.autofirst{
    margin:20px 20px 5px 20px;
    padding:10px;
    box-shadow: 0 1px 8px rgba(0,0,0,0.12), 0 1px 8px rgba(0,0,0,0.24);
    background-color:white;
    border-radius:3px;
}.item{
    padding:5px 0px;
}
</style>
文章版权声明:除非注明,否则均为折腾博客原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
验证码
评论列表 (暂无评论,24人围观)

还没有评论,来说两句吧...

取消
微信二维码
微信二维码
支付宝二维码