Webdev
Mar01
'2009

Customizing Threaded Comments in WP 2.7

虽然树型评论功能不是什么新奇的东西了,但在Wordpress2.7里作为标准功能引入,无疑还是一个非常大的进步,同时,如何根据自己的喜好修改这个评论的界面,又成了折腾的新目标。我搜了一下,网上关于这方面的介绍还并不太多,除了介绍如何在模板中打开树型评论功能支持,以及如何让模板兼容旧系统的评论功能,之外,还有都是在默认的结构上作一些css上的设置,毫无疑问那样那是头晕得很而且事倍功半的。

What if I want to make the output in my way ?

其实Wordpress的官方已经给了一个足够清楚的解释,那就是修改模板的function.php文件。首先,在你的comments.php里面用到wp_list_comments函数的地方用这样的代码:

<ol id="comment_list">
  <?php wp_list_comments('type=comment&callback=fruity_comment'); ?>
  <li class="navigation clearfix">...</li>
</ol>

问题的关键就是callback这个参数,它会调用function.php里设置的函数,比如我自己这个fruity_comment,在这里你终于可以为所欲为了。

下面就是我在function.php里面定义的相关情况:

<?php function fruity_comment($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment; ?>
  <li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
    <div id="comment-<?php comment_ID(); ?>">
      <?php echo get_avatar($comment,$size='40'); ?>
      <div class="reply">
        <h4>
          <?php comment_reply_link(
                array_merge( $args, array(
                                       'reply_text' =>'Reply',
                                       'depth' => $depth, 
                                       'max_depth' => $args['max_depth'])
                                   )
               ) ?>
        </h4>
      </div>
      <h4 class="comment-author">
        <?php printf(__('%s:'), get_comment_author_link()) ?>
      </h4>
      <?php if ($comment->comment_approved == '0') : ?>
        <em><?php _e('Your comment is awaiting moderation.') ?></em>
        <br />
      <?php endif; ?>
      <small class="comment-meta">
        <a href="<?php echo htmlspecialchars( 
                         get_comment_link( $comment->comment_ID ) ) ?>">
          <?php printf(__('%1$s'), get_comment_date()) ?>
        </a>
      </small>
      <div class="the_comment"> 
      <?php comment_text() ?>
      </div>
    </div>
<?php
        }
?>

有点乱,但是毕竟这一切都是透明的了。需要特别注意的一点,

// Do not write </li>!

Wordpress会自动把不匹配的闭合掉,这样你的评论才能层层包进来。
这样你在写css的时候肯定会得心应手多了,而且由于function.php是模板的一部分,你对评论的设置在别人那里也是肯定会被应用的。

7 Comments

  1. Madelyn Walley:

    June 21, 2011

    Please let me know if you’re looking for a writer for your blog. You have some really good articles and I believe I would be a good asset. If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine. Please blast me an e-mail if interested. Kudos!

  2. Nike Air Max Store:

    May 10, 2011

    Read again is tasted, wonderful zai! Again

  3. Burberry Store:

    May 10, 2011

    Some of us get dipped in flat, some in satin, some in

    gloss…. But every once in a while you find someone who’s

    iridescent, and when you do, nothing will ever compare

  4. Miss45:

    October 23, 2009

    Ask how long it will be before you can go back to work or start regular exercise again. ,

  5. JXL53:

    October 23, 2009

    If you do not understand the answers, ask the doctor to explain them clearly. ,

  6. Schleichs:

    July 7, 2009

    showing some love,

Leave a Comment