因为某些原因,希望自己的博客多一些评论,希望自己博客的某些资源希望评论之后才能看到,其实 WordPress 实现这个功能并不难。网上有很多类似的代码,这里直接拿过来改了下样式,先看下效果:
废话不多说,直接开始!
1. 将下面的代码添加到主题的 functions.php 文件:
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<div style="text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;>
<span class="reply-to-read">温馨提示: 此处内容需要 <a href="#respond" title="评论本文">评论本文</a> 后 <a href="javascript:window.location.reload();" target="_self">刷新本页</a> 才能查看!</span></div>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "XXX@XXX.com"; //博主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('reply', 'reply_to_read');
2. 添加短代码
将下面的代码也添加到主题的 functions.php 文件
//添加评论可见快捷标签按钮
function appthemes_add_reply() {
?>
<script type="text/javascript">
if ( typeof QTags != 'undefined' ) {
QTags.addButton( 'reply', '评论可见按钮', '' );
}
</script>
<?php
}
add_action('admin_print_footer_scripts', 'appthemes_add_reply' );
3. 使用方法:
在编辑文章时,选择 文本 编辑项,便会出现一个 评论可见按钮 如下图所示:
双击这个按钮,会在文本编辑器里出现如下代码:
注意事项:因为第一段代码设置了 博主邮箱 可见,也就是说用这个邮箱登陆的 WordPress 的账号不需要回复也就可以看到隐藏的内容,因此博主如果想看上图的效果,必须退出博主邮箱的登陆即可!
1.注明玩味资源网文章均为原创,转载请以链接形式标明本文标题和地址本文标题:wordpress教程:如何隐藏文章部分内容评论后可见【附源码】 本文地址:https://www.niuz.net/3090.html
2.本站资源来源于互联网收集/个人购买,不代表玩味资源网立场,仅供学习交流之用,切勿私自传播于网络!如有侵犯到您的权益,请联系站长解决 3.本站资源大多存储在网盘,如发现链接失效,请第一时间告知我们,我们会核实后及时更新。本站提供的源码、模板、软件工具等其他资源,都不包含技术服务,请大家谅解! 4.再次声明:本站所有资源(获得权利人授权的除外)不可用于任何商业用途,不可违反国家法律法规。使用该资源发生的一切问题与本站无关!为尊重作者版权,请购买原版作品,支持你喜欢的作者,谢谢!
评论前必须登录!
注册