10 Handy WordPress Comments Hacks

4. Use Twitter Avatars In Comments

comments hack

The problem.
Bloggers find Twitter very useful because it allows them to promote their blog and stay connected to other bloggers and their own audience. Because of Twitter’s popularity, why not illustrate comments with Twitter avatars instead of the normal gravatars?

The solution.

  1. The first thing to do is get the functions file here.
  2. Once you have that, unzip the archive to your hard drive, and then open the twittar.php file.
  3. Select all of its content and paste it to your blog’s functions.php file.
  4. The last thing to do is open your comments.php file and find the comments loop.
  5. Paste the following line in the comments loop:
    <?php twittar('45', 'default.png', '#e9e9e9', 'twitavatars', 1, 'G'); ?>

Code explanation.
Some months ago here at Smashing Magazine, an awesome plug-in named Twittar was released. Its purpose is to allow you to use Twitter avatars on your WordPress blog. Because of the number of requests I received from WpRecipes.com readers, I decided to turn the plug-in into a hack, for people who prefer hacks to plug-ins.

Of course, you could simply install the plug-in rather than insert its content into your function.php file. It’s up to you.

Sources:

5. Set Apart Author Comments With Style

comments hack

The problem.
For blog posts that have a lot of comments, finding the author’s comments and responses to reader questions is not always easy, especially if the blog don’t have WordPress 2.7’s threaded comments feature. Happily, it is possible to give author comments a different style so that readers can always quickly find your answers.

The solution.

  1. Open the comments.php file and find the comment loop:
    <?php foreach comment as $comment) { ?>
  2. After that line, paste in the following:
    <?php
    $isByAuthor = false;
    if($comment->comment_author_email == get_the_author_email()) {
    $isByAuthor = true;
    }
    ?>
  3. Once that’s done, find the line of code that represents comments (it may vary depending on your theme):
    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
  4. Now we have to output the authorcomment class if the comment was made by the author:
    <li class="<?php echo $oddcomment; ?> <?php if($isByAuthor ) {
     echo 'authorcomment';} ?>" id="comment-<?php comment_ID() ?>">
  5. The last thing we do is create a CSS class for author comments. Open the style.css file and insert the code. Replace the example colors with your colors of choice.
    .authorcomment{
    	color:#fff;
    	font-weight:bold;
    	background:#068;
    }

Code explanation.
Basically, this code compares each email address left by a commentator to the author’s email address. If they match, the $isByAuthor is set to true. When comments are displayed on screen, the value of $isByAuthor is checked. If it returns true, then the authorcomment class is added to the container.

It can be done more easily on Wordpress 2.7+ by just adding comment_class(); to the comment’s DIV, which automatically adds the class bypostauthor when you’re commenting on your own post (Thanks, Nima!).

Source:

6. Display Total Number Of Comments And Average Number Of Comments Per Post

comments hack

The problem.
On your blog’s dashboard, WordPress tells you how many total comments your blog has received. Unfortunately, there’s no function for displaying this information publicly. Displaying the total number of comments on your blog and average number of comments per post can be very helpful, especially if you have a page for advertising opportunities.

The solution.

<?php
$count_posts = wp_count_posts();
$posts = $count_posts->publish;

$count_comments = get_comment_count();
$comments  = $count_comments['approved'];

echo "There's a total of ".$comments." comments on my blog, with an average ".round($comments/$posts)." comments per post.";
?>

Code explanation.
Introduced in version 2.5, the wp_count_posts() and get_comment_count() functions allow you to easily retrieve the total number of posts and comments, respectively, on your WordPress blog. To derive the average number of comments per post, we have to do a bit of simple math, using the PHP round() function to make sure we end up with an integer.

Source:

-->