您的位置:首页»WordPress 主题, 主题开发»  WordPress2.7 嵌套评论

WordPress2.7 嵌套评论

如何使你的主题兼容 WordPress2.7 这篇文章中,我们谈到了 WordPress2.7 令人兴奋的嵌套评论功能,如果你的主题还不支持,那么我们通过本篇 WordPress 教程,一起来看看如何实现它吧!!!
WordPress2.7 使用了一个新函数–wp_list_comments,使用它,我们即可实现嵌套评论(Threading Comments)这个功能。那,我们开始改造 comments.php 文件吧:

  1. 首先,添加 comment-reply JavaScript,让嵌套评论 (Threading comments) 能够正常运行
  2. 打开 header.php 文件,在 wp_head() 函数之前添加如下函数:

    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
  3. WordPress2.7 全新的 comments loop 介绍
  4. 密码保护检查

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <?php
      if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
          die ('Please do not load this page directly. Thanks!');
      if ( post_password_required() ) { ?>
          <p class="nocomments">This post is password protected. Enter the password to view comments.</p>
      <?php
          return;
          }
    ?>

    WordPress 2.7 的评论 Loop

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?php if ( have_comments() ) : ?>
    	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>
     
    	<ol class="commentlist">
    	<?php wp_list_comments(); ?>
    	</ol>
    	<div class="navigation">
    		<div class="alignleft"><?php previous_comments_link() ?></div>
    		<div class="alignright"><?php next_comments_link() ?></div>
    	</div>
     <?php else : // this is displayed if there are no comments so far ?>
     
    	<?php if ('open' == $post->comment_status) : ?>
    		<!-- If comments are open, but there are no comments. -->
     
    	 <?php else : // comments are closed ?>
    		<!-- If comments are closed. -->
    		<p class="nocomments">Comments are closed.</p>
     
    	<?php endif; ?>
    <?php endif; ?>
  5. CSS 样式
  6. 至于 CSS 样式,各有各的喜好,大家可以借鉴 eachBlueBlueweed for WordPress2.7 这两款主题自行定义 children、depth、author。

  7. 提交嵌套评论(Threading Comments)
  8. 首先需要要把评论框 (Comment Form) 放入一个 ID 为 respond 的 DIV 中,然后找到类似以下代码:

    <input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />

    在其后面一行加入:

    <?php comment_id_fields(); ?>

    当然,假若评论者不想回复该评论了,可以加入以下代码,可以取消评论:

    1
    2
    3
    
    <div class="cancel-comment-reply">
    	<small><?php cancel_comment_reply_link(); ?></small>
    </div>

经过以上改动,我们的主题就支持 WordPress2.7 的嵌套评论(Threading Comments)功能了,使用者只需在 WordPress 管理后台激活嵌套评论即可。下一篇文章我们将一起讨论 WordPress2.7 评论分页,敬请期待。
如果你还有什么疑问,请留言或加入站趣-分享建站的乐趣 QQ 群:8329980 交流

7个评论

  • Reply 1 木子

    December 31st, 2008 at 20:20

    第二部怎么加的,还是不太懂

    • Reply 2 辐射鱼

      December 31st, 2008 at 21:58

      就是将密码保护检查、评论 loop 和 respond 衔接起来,具体你可以参考我们提供的两个主题

  • Reply 3 微尘猪猪

    April 20th, 2009 at 20:16

    绝对菜鸟 就看懂了第一步 晕死 第二步一点没明白

    • Reply 4 小影

      February 27th, 2010 at 13:41

      博主,请问如何实现你现在这种点击Reply回复框出现”to 某某某”的效果?

  • Reply 5 bolo

    June 17th, 2009 at 10:13

    好好学习,准备做主题