WordPress

给wordpress文章页增加回复可见功能

我们在写一些东西的时候,常常觉得大家都是看看,雁过不留名,那么一些内容其实是可以事先隐藏起来,通过访客成功留言后可见。上代码

第一步:在 functions.php 内加入以下代码,(注:代码里的邮箱换成自己的)

function reply_to_read($atts, $content=null) {     
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" 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 = "xxx@aaa.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');

 

演示:

[reply]当你看到这一句话的时候,回复可见就生效了[/reply]

第二步:短代码,编辑文章的时候直接用,当然了,也可以增加到编辑器中

//两种方法都可以,使用的时候去掉#
[#reply]需要回复可见的内容[/#reply]
[#reply notice="自定义的提示信息"]回复可见的内容[/#reply]

 

 

姊妹篇,密码可见

给
WordPress

给wordpress文章页增加密码可见功能

话不多说,上代码! 第一步,把下面的代码加入到你主题的functions.php里 function e_secret($atts, $content=null){ extract(shortcode_atts(array('key'=>null), $atts)); if(isset($_POST['e_secret_key']) &…

3 分钟阅读

 

龙Ge

龙Ge

前端•不会,后端•是啥

喜欢在深夜写一点没用的代码。

评论

0

发表评论

邮箱不会被公开。
验证码