Weekend Tip: Author Comment Styling In WordPress

An often-used feature in WordPress blogs is the ability to style comments made by the post’s author differently from the others – helping replies to stand out. Here, we will edit the default WordPress to add some subtle styling to author comments.

Step 1 – If Command

Open the comments.php file located in the Default’s theme directory: \wp-content\themes\default. Directly below the line:

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

There is:

<li id="comment-">

Edit this to say:

<li class="<?php echo $oddcomment; ?>
<?php if ($comment->comment_author_email == get_the_author_email()) { echo 'author_comment'; } ?>"
id="comment-<?php comment_ID() ?>">

On the second line, we have included an ‘If’ statement, which in English says: “If the email address of the comment author matches that of the post’s author (meaning it’s the same person) – then insert the text ‘author_comment’”.

Also note that we opened a class on the first line. This is because by default, the theme automatically does this for it’s ‘$oddcomment’ variable. We shall fix this now.

Step 2 – Fixing the $oddcomment

If we left our code as it is, on every ‘odd’ comment, the classes would mess up. To fix this, towards the top of the file (usually line 17), look for:

$oddcomment = 'class="alt" ';

Edit this to:

$oddcomment = 'alt';

Then further down the page (usually line 48), look for:

$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';

Edit to:

$oddcomment = ( empty( $oddcomment ) ) ? 'alt' : '';

From the past two edits, we simply removed the class=” “ text to stop any conflicts with odd comments.

Step 3 – Style It!

Open style.css in the theme directory. At the bottom, add the lines:

ol.commentlist .author_comment {
border-bottom: 1px solid #0066CC;
border-top: 1px solid #0066CC;
}

This will change the border colors on author comments (note that we’re editing the author_comment class) to a light-blue color. You may edit this however you wish, but we are simply going for a very subtle approach.

That’s It!

Preview

Related Posts

Add Comment

Discussion 56 Comments

Comment Page 2 of 2 1 2
  1. johan says:

    What if your comment.php looks nothing like that but instead a crazy assortment of this….

    This post is password protected. Enter the password to view comments.


    comment_status) : ?>

    We Have Spoken

    <div id=”comment-” class=”comment”>

    comment_approved == ‘0′) : ?>
    Your comment is awaiting moderation.

    POSTED BY: on

    comment_status) : ?>


    Comments are closed.

    You Do One Now

    <form action=”/wp-comments-post.php” method=”post” id=”commentform”>
    Name
    Email
    Site/Facebook/MySpace

    ID); ?>

    Some guy built this theme himself and I’m not sure how to get author comments styled differently. Help?

  2. Doug Sharp says:

    great tips. I enjoyed reading this,

  3. Kelly Morey says:

    Very interesting!I

  4. sooran says:

    tancks.
    This Post Helped me
    Good Time

  5. LK says:

    I would love to add this feature. However, the solution appears to be based on the assumption that there is already a solution coded to produce different styles for odd and even comments. (I implemented the comments.php from Gilles Mae, which did not include this.) I started to try this solution, but had to stop when I got to Then further down the page (usually line 48), look for:

    view plaincopy to clipboardprint?
    $oddcomment = ( empty( $oddcomment ) ) ? ‘class=”alt” ‘ : ”;

    I need more detail about the underlying odd/even solution. It would be helpful to identify insertion points by saying after such & such statement rather than line numbers. Is the entire code shown somewhere?

    Generally, appreciating the tutorials on this site, though.

  6. Bait boats says:

    Really like this site, would you mind if I link to it from my blog?

Comment Page 2 of 2 1 2

Add a Comment