Comment Design For WordPress 2.7

If you’re a WordPress theme developer, I’m sure you’ve run into the problem of styling, and altering comments in your theme. Now that comments are called through a loop to allow for the much anticipated threaded comments feature, we’ll have to make use of our function file to style the comments.

If you’re like most every other developer, the first thing you want to get rid of is the “says” that follows the authors name. Now we can do it, without having to set the CSS to display none.

01. In your theme directory, open up functions.php. If you don’t have this file, go ahead and create one.

02. Let’s create the function ‘mytheme_comment‘ and paste it into our functions.php file.

<?php
function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
         <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
         <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
      </div>
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>
      <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>
      <?php comment_text() ?>
      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
<?php
        }
?>

Looking at this code, you can see exactly what you want to do to style your comments accordingly. After you’re happy with how you want it, the next step is to create the callback from our ‘wp_list_comments’ in the comments.php file.

03. Open up comments.php in your theme directory, and search for wp_list_comments. Control + F should make short work of that.

The default theme has this exact call:

<?php wp_list_comments(); ?>

So we’ll just use the function name that we created in step 2 (mytheme_comment), and call it as a variable:

<?php wp_list_comments('callback=mytheme_comment'); ?>

And now, we’ll see the styling reflected in our comments!

Separate Comments From Trackbacks

And for another good tip, you should separate your comments from your trackbacks. It’s just good manners.

In your comments.php file you can create two lists, and call the loop twice. Only for each loop, you can specify which type of comment/trackback is called. Example:

	<h3 class="comment_title">Comments</h3>
		<ol class="commentlist">
            <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
		</ol>
 
	<h3 class="trackback_title">Trackbacks</h3>
		<ol id="pings" class="trackback">
			<?php wp_list_comments('type=pings'); ?>
		</ol>

For some complete code, and even some jQuery for a toggle hide switch on the trackbacks, be sure to read the article by Curtis Henson.

17 Comments
  1. Jauhari January 17, 20096:36 pm

    That’s I currently use too ;)

  2. ms84 March 23, 20093:46 pm

    Thanks! This really helped me! :)

  3. Dumitru Brînzan April 4, 20092:47 am

    I was looking how to make a switch between comments and trackbacks for my latest theme, this helped me a lot!

  4. Loadfuel May 15, 20094:09 am

    Brilliant.

    I have been searching for hours for a solution and i came across your fix. Thank you so much!

  5. elana May 21, 200910:17 am

    this was great – solved a couple issues i was having with the new wp 2.7 comment code

    i do have one question though, my default gravatar image got switched to the blue gravatar logo (from the mystery man)

    how do i switch that back?

    thanks!

    • elana May 21, 200910:30 am

      oooh! i got it, just removed:

      ,$default=''

    • Chad Von Lind May 21, 200910:48 am

      You’re fast. You can also use a custom image of your own, if you want to replace the ‘<path_to_url>’ with an absolute address to an image on your server.

  6. slobjones June 7, 200912:30 pm

    Everything displays fine when I make the edit to functions.php.

    However, here’s the error message I get when attempting to log in to WordPress:

    Warning: Cannot modify header information – headers already sent by (output started at /home/user/public_html/wp-content/themes/mytheme/functions.php:25) in /home/user/public_html/wp-includes/pluggable.php on line 850

    Any idea what’s causing this error? What’s in pluggable.php that creates a conflict with the mytheme_comment edit?

    Thanks.

  7. slobjones June 8, 20091:03 am

    Found the answer to my question.

    Leave no spaces between entries in functions.php!

    Thanks for the callback, Chad. This let me move my avatar outside the .vcard div so I could float it, just like you did!

  8. c0de February 23, 20105:11 am

    Doesnt work for me… dont know why but it dont will work…

    and another thing, whats the “&phpMyAdmin=446c4b004362t3f340eda” In the comment funktion? Looks like it isnt on the right place here, after “<?php" xD Gives me an Error… just wanted to say ^^

  9. c0de February 23, 20105:21 am

    Me again, sorry works fine in Orginal Version but in German Version it dont works… I’m really Happy that i only have Local the German Version ^^ Big Thx for that!

  10. Chad Von Lind February 23, 201011:29 am

    Hey C0de, I’m glad you pointed that out. It shouldn’t be there. It must have been inserted when I recently did an export and import of the database. I better check the whole database for that now. I’ve corrected the code by removing that. Danke!

  11. B4rent March 2, 20101:47 pm

    Your my HERO!!!
    Thank you!!!

  12. shulato June 17, 20108:52 pm

    thank you very much! i’m looking for this kind of tutorial. thanks for sharing

  13. WP-Central July 5, 20106:29 am

    Great share!

    Hope this works in WordPress 3.0!

Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.