Typecho1.3文章永久链接获取

在原来 Typecho1.2 中可以通过以下方式获取热门文章(评论数排序)

function GetHotPosts($limit = 10)
{
    $db = Typecho_Db::get();
    $select  = $db->select()->from('table.contents')
        ->where("table.contents.password IS NULL OR table.contents.password = ''")
        ->where('table.contents.status = ?','publish')
        ->where('table.contents.created <= ?', time())
        ->where('table.contents.type = ?', 'post')
        ->limit($limit)
        ->order('table.contents.commentsNum', Typecho_Db::SORT_DESC);
    $result = $db->fetchAll($select, array(Typecho_Widget::widget('Widget_Abstract_Contents'), 'push'));
    return $result;
}

但是 Typecho_Widget::widget('Widget_Abstract_Contents'), 'push') 在最新的 Typecho1.3 中不再支持,会获取不到文章的永久链接 permalink

使用官方推荐的函数 Helper::widgetById() 来获取文章永久链接,可以兼容 1.2 和 1.3 。

function GetHotPosts($limit = 10)
{
    $db = Typecho_Db::get();
    $select  = $db->select()->from('table.contents')
        ->where("table.contents.password IS NULL OR table.contents.password = ''")
        ->where('table.contents.status = ?','publish')
        ->where('table.contents.created <= ?', time())
        ->where('table.contents.type = ?', 'post')
        ->limit($limit)
        ->order('table.contents.commentsNum', Typecho_Db::SORT_DESC);
    $rows = $db->fetchAll($select);
    $result = [];
    foreach ($rows as $row) {
        $cid = $row['cid'];
        $post = Helper::widgetById('Contents', $cid);
        $result[] = [
            'cid' => $cid,
            'title' => $post->title,
            'permalink' => $post->permalink
        ];
    }
    return $result;
}
版权属于: 小A 本文链接: https://www.xiaoa.me/archives/typecho1_3_permalink.html 转载申明: 转载请保留本文转载地址,著作权归作者所有。

评论

等风等雨等你来