HowTo: Styling Author Comments With Wordpress 2.7+
Let’s start by looking at the code to achieve styling of author comments prior to Wordpress 2.7. On kremalicious.com I’ve used this code:
<li class="
<?php
if ($comment->comment_author_url == "http://www.kremalicious.com")
echo 'author';
else echo $oddcomment;
?>
item" id="comment-<?php comment_ID() ?>">
<em>other comments code</em>
</li>
So with some php stuff we were able to check for the author name or, as I did it, for the URL of the comment author. If one of these were detected Wordpress added a new class ‘author’ to the <li>
tag which we were able to style by adding a li.author to our css file:
li.author {
/* css comes in here */
}
But with Wordpress 2.7 these steps are needless because of the new function <?php wp_list_comments(); ?>
which adds a class on author comments for itself!
If a comment from the author of an article is posted under this article, Wordpress automatically adds the class ‘bypostauthor’ to the surrounding <li>
tag. So all you have to do is adding a css style of li.bypostauthor
to your css file or just renaming your old li.author
class or whatever you used for this:
li.bypostauthor {
/* css comes in here */
}
And that’s it for adding a different style to comments from the article author. Just add some css and there you go. Wonderful!
#Even more
Wordpress also has a special class for registered users of your site so you’re able to style their comments as well. For this just use the class ‘byuser’:
li.byuser {
/* css comes in here */
}
All the various classes Wordpress adds to comments are listed in the Codex page for enhanced comments display. And here’s a very nice grahical overview about everything Wordpress 2.7 adds to comments.
Have a comment?
Hit me up @krema@mas.to
Found something useful?
Say thanks with BTC or ETH
Edit on GitHub
Contribute to this post