修改post.php

  • 修改打开主题文件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);

$finalContent = $this->content;  // 先保存原始内容

if($this->user->hasLogin()) {
    // 处理登录可见
    $finalContent = preg_replace("/\[login\](.*?)\[\/login\]/sm",'<blockquote class="jz jc ys">$1</blockquote>', $finalContent);
} else {
    $finalContent = preg_replace("/\[login\](.*?)\[\/login\]/sm",'<blockquote class="jz jc ys">此处内容需要<a href="/admin" target="_blank">登录</a>后方可阅读</blockquote>', $finalContent);
}

// 处理回复可见
if($this->user->hasLogin() || $result) {
    // 允许查看内容
    $finalContent = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<blockquote class="jz jc ys">$1</blockquote>', $finalContent);
} else {
    $finalContent = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<blockquote class="jz jc ys">此处内容需要<a href="#comments">评论</a>回复后方可阅读</blockquote>', $finalContent);
}

echo $finalContent;
?>
<!-- 回复可见 -->

打开主题文件functions.php添加

// 回复可见
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('z97hide','one');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('z97hide','one');
class z97hide {
    public static function one($con,$obj,$text)
    {
      $text = empty($text)?$con:$text;
      if(!$obj->is('single')){
          // 直接允许查看内容
          $text = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'$1',$text);
      }
      
      return $text;
    }
}
// 登录可见
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('z97login','one');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('z97login','one');
class z97login {
    public static function one($con,$obj,$text)
    {
      $text = empty($text)?$con:$text;
      if(!$obj->is('single')){
          // 允许查看内容
          $text = preg_replace("/\[login\](.*?)\[\/login\]/sm",'$1',$text);
      }
      return $text;
    }
}

打开主题文件css添加样式

blockquote {
    margin: 0 0 1em;
    line-height: 1.8;
    font-style: oblique;
    background: #f5fafd;
    padding: 1em 1em 1em 2em;
    border-left: 5px #3498db solid;
}

在文章中使用

在需要回复查看的内容使用[#hide][/hide]包裹
在需要登录查看的内容使用[#login][/login]包裹(实际使用中去除#

此处内容需要评论回复后方可阅读