新秀网
记录生活点点滴滴

WordPress通过代码隐藏文章部分内容需评论后刷新可见

说到隐藏内容,Wordpress本身有一个自带的加密功能,就在编辑栏右边“发布”按钮的下面可以设置文章的可见性,编辑它就能,设置为输入密码浏览,缺点是整篇文章都看不到任何内容,不人性化,下边讲下通过评论文章显示隐藏部分内容的方法。

这个功能更有粘度,参与性比较强,读者喜欢的内容想看到,就必须发表自己的评论。

显示如图:

编辑当前主题functions.php,在<?php和?>之间插入以下代码即可

[huifu]

//部分内容评论可见
function huifu_to_read($atts, $content = null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read"  style="position: relative;display: inline-block;text-align: center;border: 1px dashed #ff5e52;padding: 10px;margin: 10px auto;color: #ff5e52;width: 100%;box-sizing: border-box;">温馨提示: 此处为隐藏内容需要<a href="#respond" style="text-decoration:none;" title="评论本文,后刷新即可看到!">【评论本文】</a>后刷新才能查看.</p>'), $atts));  
 $email = null;
 $user_ID = (int)wp_get_current_user()->ID;
 if ($user_ID > 0) {
 $email = get_userdata($user_ID)->user_email;
 //对博主直接显示内容
 $admin_email = get_bloginfo('admin_email');
 if ($email == $admin_email) {
 return $content;
 }
 } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
 $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
 } else {
 return $notice;
 }
 if (empty($email)) {
 return $notice;
 }
 global $wpdb;
 $post_id = get_the_ID();
 $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";
 if ($wpdb->get_results($query)) {
 return do_shortcode($content);
 } else {
 return $notice;
 }
}
add_shortcode('huifu', 'huifu_to_read');  

[/huifu]

如何调用?

在编辑文章的时候使用短代码

[huifu]需要隐藏的内容,新秀网,https://xinxiu.com.cn[/huifu]

非常给力吧,赶紧设置一下吧,只要是中间的东西都能被隐藏,包括文字、图片、代码等内容。对于一些资源性质的网站,出售VIP会员是个不错的选择。

赞(0) 打赏
未经允许不得转载:新秀网 » WordPress通过代码隐藏文章部分内容需评论后刷新可见

评论 1

评论前必须登录!

 

  1. #1

    凑个热闹

    qq4939838114年前 (2020-03-29)

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

登录

找回密码

注册