A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > WordPress > Customize Comments Markup Submit one!

Customize Comments Markup

In a typical WordPress theme you output the entire list of comments for a Post/Page by using the function wp_list_comments(). This doesn’t offer much by the way of customizing what HTML markup gets generated for that comment list. To write your own markup for the comment list, you can use a callback function as a parameter in wp_list_comments(), so it’s just as nicely abstracted.

In 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(); 

</li>
}
?>

In comments.php

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

5 Responses

  1. Ray Gulick says:

    Pasted as is, this breaks my functions file.

  2. eric says:

    God, I swear there’s like NOTHING out there that thoroughly explains comments for WP.

    • Tiffany says:

      I completely agree. I’m building my first theme and I can’t find anything on the web that gives a great “tutorial-like” breakdown of customizing the comments section. SOOOO frustrating.

  3. MikiaFto5 says:

    Good theory. I like it. Appreciate your posting

  4. Hakan says:

    thanks.. very good

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.