Once you start to receive comments on your blog you’re going to want to have your comments stand out above the crowd, after all, it is your site! You’ll notice here on Blogging Tips that if the author of the post leaves a comment on their post then it stands out with white text on a red background compared to all the other comments with black text on a white background. Whilst there are a couple of plugins out there that can do this job for you, they’re not really worth it (unless you really don’t want to delve into a bit of code!) as it only takes 1 line of code to do the job yourself.
The theory behind this is to identify whether the comment author matches the post author, and if they do then add an additional class to the comment wrap (usually a list item or div). Then you can style this class in addition to your standard comment styles.
You can identify the author using their name, email address or ID. Name is obviously not the most secure way to do it, as you will most likely have someone else with the same name as you comment on your site. Using the email address is a popular method too, but what happens when you change your email address? Your older comments will no longer be highlighted. You could do a search through the database to update these but why bother? Using the ID is the most accurate and secure method. To do this you need to open your comments.php template file. Locate the line which wraps the comment. In the Classic theme for example this is a list item as follows:
<?php foreach ($comments as $comment) : ?>
<li id="comment-<?php comment_ID() ?>">
This is at line 14/15 in the Classic comments.php theme file. So here we need to add an additional class to the list item to identify the Author’s post. So change the second line above to:
<li id="comment-<?php comment_ID() ?>"<?php if ($comment->user_id == get_the_author_ID()) echo ' class="mycomment"'; ?>>
This will now add a class of ‘mycomment’ to any list item that is your comment. You can then style the mycomment class in your CSS file to distinguish it. Don’t forget to put the mycomment class styles after the comment class styles, as you need the mycomment class to override the comment class.