Typecho Wiki
每一个作品都值得被记录

Typecho 非插件实现热评文章调用

Typecho维基君Typecho教程 • 1824次浏览 • 发布 2018-05-16 • 更新 2018-05-16
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

热评文章即网站中被评论次数最多的文章,一般热评文章、热门文章和随机文章是网站常用的排行文章模块,那么 typecho 网站要如何在页面中显示评论最多的文章列表?

1、在当前主题的 functions.php 文件中添加以下函数代码:

function getHotComments($limit = 10){
    $db = Typecho_Db::get();
    $result = $db->fetchAll($db->select()->from('table.contents')
        ->where('status = ?','publish')
        ->where('type = ?', 'post')
        ->where('created <= unix_timestamp(now())', 'post') //添加这一句避免未达到时间的文章提前曝光
        ->limit($limit)
        ->order('commentsNum', Typecho_Db::SORT_DESC)
    );
    if($result){
        foreach($result as $val){            
            $val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
            $post_title = htmlspecialchars($val['title']);
            $permalink = $val['permalink'];
            echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';        
        }
    }
}

2、在要调用热评文章位置对应的模板文件(如index.php、single.php、sidebar.php或page.php等)添加调用代码:

<?php getHotComments('10');?>

代码中的10表示要调用的文章数量。

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:typecho
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »