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

971 次浏览次阅读
一条评论

共计 1440 个字符,预计需要花费 4 分钟才能阅读完成。

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

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 会员是个不错的选择。

正文完
 
新秀网
版权声明:本站原创文章,由 新秀网 2020-03-23发表,共计1440字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(一条评论)
2020-03-29 17:07:02 回复

凑个热闹

 Windows  Chrome  中国湖南省怀化市电信