Typecho Wiki
每一个作品都值得被记录

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

Typecho维基君Typecho教程 • 1988次浏览 • 发布 2018-07-11 • 更新 2019-03-16
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

因为一般首页很少有全文输出的,而且一般隐藏内容都放在文章后面,所以就不需要处理首页的输出内容了。

直接处理post.php就可以了,使用下面的代码

<?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 
?>

替换模板post.php中的

<?php $this->content(); ?>

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

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

css参考样式

.reply2view {
    background:#f8f8f8;
    padding:10px 10px 10px 40px;
    position:relative
}

上面的评论回复可见会有一个BUG,就是当你的回复可见的内容正好在文章开头,这样Typecho调取文章摘要时就会暴露隐藏的内容,最近看到QQdie解决了这个BUG,所以我下面也就直接转载他的内容了:

第一步:将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
}

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:typecho
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »