先加入funtion代码
// 未登录提示卡片 即登录可见 add_shortcode('hide', 'loginvisible'); function loginvisible($atts, $content = null) { if (is_user_logged_in() && !is_null($content) && !is_feed()) { return $content; } $out = ' <div class="post-hidden-tips"> <div class="hidden-blur-poster" style="background-image: url(https://lz.sinaimg.cn/mw690/73f6551dly1hd901lbkzwj207m03k0dz.jpg);"></div> <div class="hidden-tips-text">此处内容需要<a style="color: #000; font-weight: 600;" href="/wp-login.php" title="注册登录"> 注册登录</a> 后才能查看隐藏内容</div> </div>'; return $out; } // 回复可见 add_shortcode('reply', 'reply_to_read'); function reply_to_read($atts, $content = null) { $out = ' <div class="post-hidden-tips"> <div class="hidden-blur-poster" style="background-image: url(https://lz.sinaimg.cn/mw690/73f6551dly1hd901lbkzwj207m03k0dz.jpg);"></div> <div class="hidden-tips-text">此处内容需要<a style="color: #000; font-weight: 600;" href="' . get_permalink() . '#respond" title="评论本文"> 评论本文</a> 后刷新本页才能查看.</div> </div>'; extract(shortcode_atts(array("notice" => $out), $atts)); $email = null; $user_ID = (int)wp_get_current_user()->ID; if ($user_ID > 0) { $email = get_userdata($user_ID)->user_email; //如果用户已登录,从登录信息中获取email } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) { $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]); //如果用户未登录但电脑上有本站的Cookie信息,从Cookie里读取email } else { return $notice; //无法获取email,直接返回提示信息 } if (empty($email)) { return $notice; } // 已做缓存 https://m.wpjam.com/article/wordpress-transients-api/ // 6个小时查询一次 global $wpdb; $post_id = get_the_ID(); //文章的ID $reply_to_read = get_transient('reply_to_read_' . $email . '_' . $post_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 ($reply_to_read) { return $content; } else { if ($wpdb->get_results($query)) { set_transient('$reply_to_read'. $email . '_' . $post_id, true, 60 * 60 * 6); return $content; //查询到对应的已经审核通过的评论则返回内容 } else { return $notice; //否则返回提示信息 } } }
其次引入CSS美化代码
/* 回复模块 */ .post-hidden-tips { position: relative; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-align: center; -ms-flex-align: center; align-items: center; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; -webkit-box-sizing: border-box; box-sizing: border-box; text-align: center; font-size: 14px; padding: 4rem; margin: 2rem 0; border-radius: var(--radius-wrap); } .hidden-blur-poster { position: absolute; right: 0; bottom: 0; top: 0; left: 0; background-position: center; background-repeat: no-repeat; background-size: cover; border-radius: inherit; opacity: .4; } .post-hidden-tips .hidden-tips-text { position: relative; -webkit-box-shadow: inset 0 -2px 0 var(--Maincolor); box-shadow: inset 0 -2px 0 var(--Maincolor);; } .post-hidden-tips .hidden-tips-text .hidden-tips-arrow { position: absolute; width: 20px; height: 20px; border-radius: 100%; top: 50%; right: 0; background: var(--Maincolor); -webkit-transform: translate(150%,-50%); -ms-transform: translate(150%,-50%); transform: translate(150%,-50%); }
最后是插入代码,效果如下图
评论回复可见效果演示
[reply]在这里填入内容[/reply]
隐藏内容登录可见效果演示
[hide]在这里填入内容[/hide]