赞助展示位
立即入驻

WordPress网站添加回复可见内容

教程2年前 (2022)更新 终极
512 0 0

functions.php

在主题根目录下的functions.php文件中添加以下代码:

//部分内容评论可见  
function reply_to_read($atts, $content = null) {  
 extract(shortcode_atts(array(  
 "notice" => '<div class="reply">隐藏内容,回复可见“<a href="' . get_permalink() . '#respond" title="回复可见">回复本文</a>”后“<a href="javascript:window.location.reload();" title="刷新页面">刷新页面</a>”查看隐藏内容!</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 = 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 (emptyempty($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');  

接着需要在编辑器中添加自定义按钮functions.php添加以下代码:

 //添加HTML编辑器自定义快捷标签按钮  
add_action('after_wp_tiny_mce', 'bolo_after_wp_tiny_mce');  
function bolo_after_wp_tiny_mce($mce_settings) {  
?>  
<script type="text/javascript">  
QTags.addButton( 'reply', '回复可见', '[@reply]\n\n[/reply]\n', "" );  
</script>  
<?php  
}  

以上代码其中[@reply]中的@去掉

style.css

在主题根目录下的style.css文件中添加以下代码:

/*回复可见*/
.reply {
  padding: 10px;
  border: 1px #49aff9 dashed;
  background-color: #fafafa;
  color: #f40000;
  text-align: center;
  font-size: 14px;
}

以上代码根据自己喜欢可以适当修改

调用代码

在文本编辑框可看到回复可见按钮,插入即可,或者输入[@reply][/reply],其中@删除掉

效果演示

WordPress网站添加回复可见内容
© 版权声明

相关文章

暂无评论

暂无评论...
终极导航 一键登录开启个人书签等功能!