说到隐藏部分文章内容,这种做法常见于论坛,在wordpress博客程序中用得其实并不广泛。当然,这也与两者的用户群体以及定位不同;论坛经常设置部分内容隐藏、内容登陆、回复可见等限制,主要是为了吸引用户注册,而对于wordpress博主而言,这个目的并不是很强烈,所以对这个功能的需求也相对小很多。但是,随着wordpress的盛行,使用wordpress建站的类型以及用途越来越多元化,而wordpress本帖隐藏的内容需要回复才可以浏览等这些功能也能起到一定的作用,下面就分享下2种简单的方法来实现这个功能!
一、通过修改代码实现
个人觉得wordpress插件来实现是很简单方法,但是大家都知道wordpress插件安装过多的话,不仅会影响网站的整体访问速度,同时网络上的免费wordpress插件安全性也有待考量,如果你不患有代码恐惧症,还是自己倒腾吧,其实操作起来也很简单!只需要将以下隐藏内容回复可见代码加入到当前使用的主题的functions.php文件里即可。
////回复可见///// function reply($content){ if (preg_match_all('/<!--reply start-->([sS]*?)<!--reply end-->/i', $content, $hide_words)){ $stats = 'hide'; global $current_user; get_currentuserinfo(); if ($current_user->ID) { $email = $current_user->user_email; } else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) { $email = $_COOKIE['comment_author_email_'.COOKIEHASH]; } $ereg = "^[_.a-z0-9][emailprotected]([0-9a-z][0-9a-z-]+.)+[a-z]{2,5}$"; if (eregi($ereg, $email)) { global $wpdb; global $id; $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'"); if ($comments) { $stats = 'show'; } } $admin_email = "[emailprotected]"; //博主Email,博主直接查看 if ($email == $admin_email) { $stats = 'show'; } $hide_notice = '<div>温馨提示:此处内容需要<a href="'.%20get_permalink().'#respond" title="评论本文">评论本文</a>后,<a href="https://www.xhsay.com/javascript:window.location.reload();" title="刷新">刷新本页</a>才能查看。</div>'; if( $stats == 'show' ){ $content = str_replace($hide_words[0], $hide_words[1], $content); }else{ $content = str_replace($hide_words[0], $hide_notice, $content); } } return $content; } add_filter('the_content', 'reply');
然后打开你的wordpress后台——外观——编辑,找到右侧的funtion.php——点击打开,然后把上面的代码黏贴到function.php的最后——点击更新
在发表博文,在你想要隐藏的某部分内容中加入
<!–reply start–>隐藏内容<!–reply end–> 【需要你使用代码模式中输入】
PS:(1)请将管理员的E-mail改成自己管理账户的E-mail,这样你可以不用回复就可见。
(2)另外,目前BUG是,使用多说等评论插件无效,只支持主题内置留言。
二、通过wordpress插件实现
(1)效果预览
其实隐藏部分内容还是挺简单的!
(2)安装方法:
1. 直接在wordpress后台插件搜索安装“login to view all”或者去wordpress官网下载插件,解压缩,你将会看到一个文件夹login-to-view-all,然后将其放置到插件目录下,插件目录通常是 /wp-content/plugins/
2. 在后台对应的插件管理页激活该插件Login to view all
4. 完成
(3)使用说明:
1. 在WordPress后台编辑文章的时候,切换到HTML模式,选中你要隐藏的内容,点击按钮 loginview 即可用
<!–loginview start–> 隐藏内容<!–loginview end–>
将隐藏内容括起来;使用这个标签的好处是,你停用本插件后,该标签不会被显示出来。
2. 这样未登录的用户浏览文章的时候,将无法阅读隐藏的内容。