Typecho完美实现回复可见功能
时间:2019-02-13 16:48:40
之前发布了《typecho非插件实现评论回复可见》,可以实现回复可见功能,但是有个问题,在文章列表页展示文章缩略内容时,如果回复可见内容刚好在缩略内容的位置上时,就会暴露出来,同时Feed里面也会暴露这个问题,那么如何解决呢,下面请看如何几近完美的实现回复可见功能:
第一步:
将post.php
中的<?php $this->content(); ?>
换成
<?php $db = Typecho_Db::get(); $sql = $db->select()->from('table.comments') ->where('cid = ?',$this->cid) ->where('mail = ?', $this->remember('mail',true)) ->limit(1); $result = $db->fetchAll($sql); if($this->user->hasLogin() || $result) { $content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">$1</div>',$this->content); } else{ $content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">此处内容需要评论回复后方可阅读。</div>',$this->content); } echo $content ?>
第二步:
解决缩略内容和feed暴露问题。
在functions.php
中加入如下代码即可
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('myyodux','one'); Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('myyodux','one'); class myyodux { public static function one($con,$obj,$text) { $text = empty($text)?$con:$text; if(!$obj->is('single')){ $text = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'',$text); } return $text; } }
就是用插件接口,在缩略内容输出之前,隐藏掉或者替换掉回复可见内容,同时使用if判断,来针对非single
页面进行隐藏。
使用方法:
在写文章需要隐藏部分内容时用以下写法(去掉@)
[@hide]要隐藏的内容[/hide]
CSS参考样式
.reply2view { background:#f8f8f8; padding:10px 10px 10px 40px; position:relative }
结语
这个新方法实现评论回复可见,来自泽泽博客,感觉有用 收藏一下。