Nov
30
2008

How to Create a Custom Style for Admin Comments in WordPress


I noticed how on some WordPress blogs authors’ comments are styled differently from other people’s comments. I think this really makes it easier when users are scanning through your comments, looking for an actual moderator/author/admin’s response. Making your WordPress install style your author comments differently is quite a simple little coding modification where you only need to change a small piece of code in one file and add a style class to your CSS stylesheet.

This works only on WordPress 2.5.

Open up your themes comments.php file and look for line 27. Should look something like this…

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php foreach ($comments as $comment) : ?>
 
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<cite><?php comment_author_link() ?></cite> Says:
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<br />
 
<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?></small>
 
<?php comment_text() ?>
 
</li>

All you need to do is add is a little if statement to check if your author is an admin, and then a few more things. Copy the code below and paste it over the code above.

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php foreach ($comments as $comment) : ?>
<?php if (1==$comment->user_id) { ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<div class="authorcomment"></div>
 
<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?></small>
 
<?php comment_text() ?>
 
</li>
<?php } else { ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<cite><?php comment_author_link() ?></cite> Says:
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?>
<br />
 
<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?></small>
 
<?php comment_text() ?>
 
</li>
<?php } ?>

Then just add the “authorcomment” class to your CSS stylesheet and style it however you choose. Mine looks something like this.

.authorcomment {
background: url(images/bavota.png) no-repeat;
float: left;
width: 89px;
height: 97px;
}

See below how my comments have the “B” logo on the left. You can add a background color or place the image differently or do whatever you choose to have a custom style for your author comments.

If you liked this, please share it.

Tags: , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/bKVPJ6

Discussion 4 Comments

  1. c.bavota on November 28, 2008 at 6:35 am

    No prob. You can also make your comments stand out by completely customizing what information appears. Just change the code between lines 29 and 36 to make the information your want to be displayed show up. Let me know if you want something specific and I’ll see if I can help out.

  2. Daniel Hunninghake on August 11, 2009 at 11:13 am

    Thank you, very helpful!
    However, there’s a big error: Right now, on line 30, you close the authorcomment div class right away, so it’s not actually being applied to anything.

    To fix that, merely shift the closing on line 30 down to line 35 above . That way, it gets applied to the author’s comment.
    Thanks again!

  3. resume on March 4, 2010 at 8:12 am

    This code doesn’t work, at what can be a problem ???

    • c.bavota on March 6, 2010 at 10:50 am

      This tutorial is from 2008 and worked with WordPress 2.5. The latest version of WordPress works quite differently. I will have to update it when I have the chance.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.