调用独立页面评论

721天前2310

Typecho调用某个独立页面的评论并输出 li 标签,网上看到的,特意记录下,当然我觉得可以把自己的时光机页面调用到首页做个滚动的公告栏或者展示啥的,哈哈。

<?php
        $slug = "links"; //独立页面缩略名
        $limit = 8;  //调用评论数量
        $length = 30;  //截取文字长度
        $ispage = true;  //true 输出页面评论,false 输出其它所有评论
        $isGuestbook = $ispage ? " = " : " <> ";
        $db = $this->db;//Typecho_Db::get();
        $options = $this->options;//Typecho_Widget::widget('Widget_Options');
        $page = $db->fetchRow($db->select()->from('table.contents')
            ->where('table.contents.status = ?', 'publish')
            ->where('table.contents.created < ?', $options->gmtTime)
            ->where('table.contents.slug = ?', $slug));
        if( $page ){
            $type = $page['type'];
            $routeExists = (NULL != Typecho_Router::get($type));
            $page['pathinfo'] = $routeExists ? Typecho_Router::url($type, $page) : '#';
            $page['permalink'] = Typecho_Common::url($page['pathinfo'], $options->index);
            $comments = $db->fetchAll($db->select()->from('table.comments')
            ->where('table.comments.status = ?', 'approved')
            ->where('table.comments.created < ?', $options->gmtTime)
            ->where('table.comments.type = ?', 'comment')
            ->where('table.comments.cid '.$isGuestbook.' ?', $page['cid'])
            ->order('table.comments.created', Typecho_Db::SORT_DESC)
            ->limit($limit)  );
            foreach($comments AS $comment) {
                echo '<li>';
                echo '<a href="'. $page['permalink']."#comment-".$comment['coid'] .'" title="'.$comment['text'].'">';
                echo Typecho_Common::subStr(strip_tags($comment['text']), 0, $length, '...').'</a>';
                echo '</li>';
            }    
        }else{
            echo "<li>No Comments</li>";        
        }
?>

在需要展示的地方插入上面的代码即可,当然这个代码还是比较啰嗦的,可自行删减使用,其实只要一句查询一句输出就行了,哈哈。

当然,你也可以把上面的代码封成 function,在多个地方调用不同的单页评论,其原理就是查询数据库评论表的 text 字段,进行循环输出,结束。

* 若非特殊说明,本站文章均为博主原创,码字不易,如需转载,请注明出处!有疑问可留言交流,谢谢。

PHPTypecho19 

调用独立页面评论 - Jdeal | Life is like a Design.