Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Jalenack/Comment Loop Beauty

Highlighting Blog Author Comments

As a blog author, you may want your name to stand out from the rest of your commenters. WordPress handily stores the user ID of each comment (if they don't have one, it defaults to '0'). Using this information, it's possible to differentiate the blog admin from other users. Let's say you wanted your name to be highlighted. To do this, let's open up comments.php of your current Theme, and look for the comments loop. The comments loop starts like this:

<?php foreach ($comments as $comment) : ?>

Anything inbetween that opening tag and <?php endforeach; /* end for each comment */ ?> will be repeated for every comment . Now, somewhere inbetween there you've probably got a reference to <?php comment_author() ?>. This code will print the comment author's name. Now, in order to highlight them, change:

<cite><?php comment_author() ?></cite> Says:


<cite<?php if ($comment->user_id == '1') echo ' class="admin"'; ?>>
	<?php comment_author() ?>
</cite> Says:

The above code example will output *Admin user Name* Says: if the comment was written by the blog admin, whose user_id is '1'. In order to style that, you can add something like this to your CSS stylesheet:

.admin {
font-weight: bold;

This will make the name of the blog admin bold. Also, note that you can highlight all registered users. The following code will simply check for the existence of any registered user:

<?php if ($comment->user_id) echo ' class="admin"'; ?>

Other examples

Need some examples....

Other things to write about:

  • Apply class to registered commentors with a specific role, i.e., all administrators.
  • Having comment number counts for each comment.
  • Styling pingbacks and trackbacks specially.
  • Separating comments from trackpings.