基于正则表达式隐藏博客内容
目前 Typecho 只支持将整个文章的内容隐藏,很多时候只需要隐藏文章的部分内容,既然官方没有提供这个功能,那就自己实现一下。
设计思路也很简单:
- 设定隐藏标记,在隐藏标记之间的内容被隐藏
- 解析文章内容,若当前用户可以访问隐藏内容,则将隐藏标记去掉
- 解析文章内容,若当前用户不可以访问隐藏内容,则将隐藏标记去掉同时替换隐藏标记之间的文本
您需要回复才能显示此处隐藏内容。
最终实现效果如下:在写博客时,只需要遵守下面的语法就可以实现隐藏内容的效果。
[hide]
该部分内容需回复可见
[/hide]
[login]
该部分内容仅登录可见
[/login]
插件的使用方法如下:
- 替换
post.php
中文章内容输出语句 - 自定义登录点击事件(可选)
首先是替换内容输出语句,在主题文件夹下post.php
中找到文章内容输出语句,以default
主题为例:
<div class="post-content" itemprop="articleBody">
<?php parseContnet($this->content); ?>
</div>
我们在div
区块间加入文章内容替换语句
<?php
if (array_key_exists('Hide', Typecho_Plugin::export()['activated'])) {
$this->content = Hide_Plugin::parse_content($this->content, $this->cid, $this->remember('mail',true), $this->user->hasLogin(), $this->options->adminUrl);
}
?>
最后post.php
文件内容如下(仅输出文章内容部分):
<div class="post-content" itemprop="articleBody">
<?php
if (array_key_exists('Hide', Typecho_Plugin::export()['activated'])) {
$this->content = Hide_Plugin::parse_content($this->content, $this->cid, $this->remember('mail',true), $this->user->hasLogin(), $this->options->adminUrl);
}
?>
<?php parseContnet($this->content); ?>
</div>
然后需要绑定评论点击事件,事件需要被添加在footer.php
中<body>
块中
<?php if (array_key_exists('Hide', Typecho_Plugin::export()['activated'])) : Hide_Plugin::commentClick(); endif; ?>
以default
主题为例,可在如下位置插入点击事件语句:
<?php $this->footer(); ?>
<?php if (array_key_exists('Hide', Typecho_Plugin::export()['activated'])) : Hide_Plugin::commentClick(); endif; ?>
</body>
当内容被隐藏时评论才可见的HTML
代码如下:
<div class="reply2view" style="font-weight:bolder; color:#336699;">您需要<a id="comment_show" href="#comments">回复</a>才能显示此处隐藏内容。</div>
我们需要根据这部分代码设计回复点击事件,回复点击事件就是点击回复之后会打开评论框,需要根据使用的主题进行微调,同时注意在配置界面填写点击事件代码时<script>
与</script>
不可省略。
<script>
$("#comment_show").click(function() {
$("#comments").fadeIn(1000);
});
</script>
如果你认为这篇文章还不错,可以考虑 为作者充电 ⚡️