钧言极客站钧言极客

钧言极客

Typecho 非插件实现内容回复可见功能

之前看见WP的博客,部分内容都带着阅读回复可见的功能。所以在网上找了很多的版本,所以这个也是从网上搬来的。这个方法已经测试过可行,分享给你们。来自: typecho wiki

进入 网站目录/usr/themes/正在使用主题 找到 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);
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('moleft','one');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('moleft','one');
class moleft {
    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-color: rgba(0,0,0,.075);
    border-radius: 5px;
    border: 1px dashed #888888;
    position: relative;
    text-align: center;
    padding: 10px 20px;
}
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Typecho 非插件实现内容回复可见功能》
文章链接:https://www.jinjun.top/336.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 1

  1. 这个不错,不要插件,又不影响速度。

    老张博客 2020年12月02日    回复