自定义评论标记

Avatar of Chris Coyier
Chris Coyier

在典型的 WordPress 主题中,您可以使用函数 `wp_list_comments()` 输出文章/页面的整个评论列表。但这对于自定义为该评论列表生成的 HTML 标记来说,并没有提供太多帮助。要为评论列表编写您自己的标记,您可以使用回调函数作为 `wp_list_comments()` 中的参数,因此它同样得到了很好的抽象。

在 `functions.php` 中

<?php
function my_custom_comments($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
   <?php if ($comment->comment_approved == '0') : ?>
      <em><?php _e('Your comment is awaiting moderation.') ?></em>
   <?php endif; ?>

   // Comments markup code here, e.g. functions like comment_text(); 

}
?>

在 `comments.php` 中

<?php 
   wp_list_comments("callback=my_custom_comments"); 
?>