908° 2011954字0条评论

隐藏已知评论者信息

在之前的文章Ajax 嵌套评论中,我介绍了如何不用刷新页面就能预提交评论,而且可以在刷新前重新编辑。其实我一直觉得,隐藏已知评论者信息并显示欢迎回来字样的样式跟 ajax 嵌套评论就是一对儿!所有为了寻觅伴侣,我先是找到奚少修改过的万戈的办法,可是不支持我现在的主题。原因很简单,现在很多主题都使用 < ?php comment_form(); ?> 来实现模板评论的,所以具体信息无法修改。不过皇天不负有心人,我找到了这篇文章 Comment_form()下实现隐藏评论者信息(链接失效),在装卸两个来回之后,终于搞定。这里还要感谢原文作者 Harry Lyn 的热情帮助。出来的效果如图所示,或者你也可以直接测试留言

隐藏已知评论者信息
隐藏已知评论者信息

第一步

comment.php 里找到 < ?php comment_form(); ?>,替换为以下代码:

< ?php $fields = array(
'author' =>$hideauthor.   '<div id="author_info">' . '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p></div>', );
$args = array(
'fields' => apply_filters( 'comment_form_default_fields', $fields ));
comment_form($args); ?>

第二步

在前面再加上一段如下的“欢迎信息”,具体可以自己看着修改,然后保存 comment.php

< ?php if ( $comment_author != "" ) : ?>
< ?php $hideauthor = '<div id="welcome">欢迎回来,<strong>' .$comment_author. '</strong> '?>
< ?php endif; ?>

第三步

下载 infohide.js 或者新建一个名为 infohide 的 js 文件,内容如下,然后上传到主题目录下:

$(document).ready(function() { //开始
if($('#author').val()!=""){ //判断用户框是否有值
$("#author_info").css('display','none'); //将id为author_info的对象的display属性设为none,即隐藏
var change='<span id="show_author_info" style="cursor: pointer; color:#39f;">修改 &raquo;</span>'; //定义change,style是定义CSS样式,让他有超链接的效果,color要根据你自己的来改,当然你也可以在CSS中定义#show_author_info来实现,这样是为了不用再去修改style.css而已!
var close='<span id="hide_author_info" style="cursor: pointer;color:#39f;">完成 &raquo;</span>'; //定义close
$('#welcome').append(change); //在ID为welcome的对象里添加刚刚定义的change
$('#welcome').append(close); // 添加close
$('#hide_author_info').css('display','none'); //隐藏close
$('#show_author_info').click(function() { //鼠标点击change时发生的事件
$('#author_info').slideDown('slow'); //用户输入框向下滑出
$('#show_author_info').css('display','none'); //隐藏change
$('#hide_author_info').css('display','inline'); //显示close
$('#hide_author_info').click(function() { // 鼠标点击close时发生的事件
$('#author_info').slideUp('slow'); //用户输入框向上滑
$('#hide_author_info').css('display','none'); //隐藏close
$('#show_author_info').css('display','inline'); })})}}) //显示change

第四步

header.php 或者 footer.php 中引用以上 infohide.js(确保 jQuery 在前面):

< ?php if ( is_singular() ){ ?>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/infohide.js"></script>
< ?php } ?>

如果本身没有使用 jQuery,就在上述代码前加上以下代码并保存:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

无法工作的话请检查并排除以下几种情况:

  1. infohide.js 文件中的第二行是 if($('#author').val()!=""){ 而不是 if($('input#author[value]').length>0){
  2. 上述代码中的 " ' 等符号为英文符号,而不是中文的 “” ‘
  3. 检查自身引用的 jQuery 版本,根据反馈,1.3.2版本的 jQuery 无法与之正常工作。※上述引用的版本是1.2.6,本博使用的是1.2.3。
EOF
721°
添加谷歌广告
Comments
Write a Comment
点击加载Disqus