notice
当前位置: 首页>>Wordpress>>主题>> 阅读正文

使主题支持嵌套评论

发表评论 | Trackback 2009年8月21日 作者:死兔子 点击量:3,879 次

现在的主题的评论有两种方法显示回复评论,一种是嵌套评论,一层评论套一层评论回复。一种是@评论,就是以@+评论人名的方式回复。其实两者都有优缺点,前者的优点就是清楚的看出回复所针对的内容!缺点是极大的破坏了评论的显示布局。后者是充分的保证了评论的显示布局,使之主题更佳美观!缺点是如果多评论的话,不容易让人了解究竟回复的是什么内容。 接下来我们来讲讲怎么嵌套评论!

在 WordPress 2.7 之前,要使主题支持嵌套评论,我们必须开启 WordPress Thread Comment这个插件,而在即将发布的 WordPress 2.7 中,Thread Comment 将被集成进来,使用 wp_list_comments函数即可调用嵌套评论(Thread Comment)。想要使主题支持嵌套评论(Thread Comment)这个功能吗?那就继续看下去吧。

1.添加 comment-reply JavaScript

要使主题支持嵌套评论(Thread comment),那么就要在 wp_head() 函数之前添加如下函数:

<?php if(is_singular()) wp_enqueue_script( 'comment-reply' ); ?>

其作用是加载嵌套回复所需的 JavaScript 代码. (也就是说, 如果浏览器不支持 JavaScript, 嵌套回复就没法实现)

2.判断 WordPress 版本

要使主题支持嵌套评论,那么要有选择性地使用 wp_list_comments 函数,WordPress 2.7 通过函数 wp_list_comments 来显示所有留言,可之前版本并没有这个函数,所以,我们需在 comments.php 文件中添加如下代码:

<?php if (function_exists('wp_list_comments')) : ?>
// new comments.php stuff ;
<?php else : ?>
// old comments.php stuff endif;

3.WordPress 2.7 的评论 Loop

<?php if ($comments) : ?>
<span><?php comments_number('No Comments', 'One Comment', '% Comments' ); ?></span>
<div class="comment_list">
    <?php wp_list_comments(); ?>
    <?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; ?>
</div>

4.设置评论框

需要要把评论框 (Comment Form) 放入一个 ID 为 respond 的 DIV 中,然后并在评论框中添加如下代码:

<?php comment_id_fields(); ?>

当然,我们也可以取消回复

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

接下来就是css部分的美化了,还有就是要wordpress后台讨论中选择支持嵌套评论,不然搞了这么多也不会有嵌套评论出现的! 结果主题就可以支持嵌套评论了

anyShare分享到:
          
发表评论 | Trackback 0 条评论.
目前还没有任何评论.
icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif