Last night I was working on styling comments in the upcoming version of Marketers Delight and I discovered something that’s always been a curiosity in the back of my mind.
If you’re familiar with the WordPress comments system, you’ve more than likely styled author comments by targeting the
bypostauthor class WordPress automatically outputs to author comments.
This class is very useful as you can write custom CSS to make an author comment look different from everyone else’s comments. For example, you could change the background or border color to make it stand out a little more.
The problem is, it’s just CSS. If you wanted to do something like add a text badge next to the author’s comment that says something like “Post Author”, you’d either have to write a bunch of pseudo CSS or write some strange PHP (I’ve Googled this, and there aren’t any *great* answers).
A Simple Solution
WordPress provides us with the useful
get_comment_class() function (to be used in the comment function you set in the
wp_list_comments() call). As the Developer Reference describes it:
Returns the classes for the comment div as an array.
Return: (array) An array of classes.
…since it returns an array, you can easily check the data it returns by placing this somewhere in your comment function (make sure you delete it when you’re done, this is strictly for learning purposes):
<?php print_r( get_comment_class() ); ?>
and for each comment, you’ll see an array structure something like this:
Array (  => comment  => byuser  => comment-author-username  => bypostauthor  => odd  => alt  => depth-2 )
Your results will vary slightly, but if you take a look at any of the author’s comments results, you’ll find that the
bypostauthor class is assigned to the array.
With this in mind, we can come up with a simple check with PHP to determine whether the current comment is the author’s or not:
<?php if ( in_array( 'bypostauthor', get_comment_class() ) ) : ?> ...html here... <?php endif; ?>
All this check does is search the array (
get_comment_class()) for the
bypostauthor value. If it’s not there, the condition will return false and no HTML will be output (learn more about the
Using this conditional you can add something like the text badge I mentioned above or even completely manipulate the entire author’s comment to differentiate from the rest of your comments.
To get an idea of what you could do with it, I’ve left a comment below this post to show you what author comments in MD look like. Happy commenting!