Hi!请登陆

Typecho回复可见功能的实现方法

2020-10-17 87 10/17

不需要通过插件,就开源完美实现Typecho回复可见功能的方法

最近逛博客,发现大家的博客都有回复可见的功能
回到我的博客一看发现没有,这可不得行
于是我也整了一个233,顺便分享给大家

步骤
将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))
    ->where('status = ?', 'approved')
//只有通过审核的评论才能看回复可见内容
    ->limit(1);
$result = $db->fetchAll($sql);
if($this->user->hasLogin() || $result) {
    $content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view jz jc ys">$1</div>',$this->content);
}
else{
    $content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view jz jc ys">此处内容需要评论回复后(审核通过)方可阅读</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页面进行隐藏↑

添加 CSS 样式
↓CSS 参考样式↓

↓样式如下↓

.reply2view {
    background:#66ccff;
    padding:10px 10px 10px 40px;
    position:relative
}
.jz{text-align:center;}
.jc{ font-weight:bolder;}
.ys{color:#336699}

使用
↓在写文章需要隐藏部分内容时用以下写法(去掉@)↓

[@hide]要隐藏的内容[/hide]

相关推荐