东风不来
三月的柳絮不飞

WordPress隐藏文章部分内容评论后可见

因为某些原因,希望自己的博客多一些评论,希望自己博客的某些资源希望评论之后才能看到,其实 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', '评论可见按钮', '【reply】','【/reply】' );
        } 
    </script>
<?php 
}
add_action('admin_print_footer_scripts', 'appthemes_add_reply' );

[qgg_yellow]注意:添加短代码时【】全部替换成[][/qgg_yellow]

3. 使用方法:

在编辑文章时,选择 文本 编辑项,便会出现一个 评论可见按钮 如下图所示:

双击这个按钮,会在文本编辑器里出现如下代码:

4. 效果


[qgg_yellow]注意事项:因为第一段代码设置了 博主邮箱 可见,也就是说用这个邮箱登陆的 WordPress 的账号不需要回复也就可以看到隐藏的内容,因此博主如果想看上图的效果,必须退出博主邮箱的登陆即可![/qgg_yellow]
教程完毕!

赞(1) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《WordPress隐藏文章部分内容评论后可见》
文章链接:https://www.wenzika.com/584.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 3

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    有没有登录可见啊

    佛系软件 3年前 (2021-03-17) 来自天朝的朋友 谷歌浏览器 Linux 回复
  2. #0

    怎么设置游客回复可见啊大神

    佛系软件 4年前 (2020-04-10) 来自天朝的朋友 谷歌浏览器 Mac OS X 10_15_4 回复
  3. #0

    666666

    云梦端 5年前 (2019-03-11) 来自天朝的朋友 谷歌浏览器 Windows 7 回复

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

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

支付宝扫一扫打赏

微信扫一扫打赏