24Mar

12 Comments

Display recent comments with Gravatars – No Plugin | Wordpress Wednesday #1

Posted on March 24, 2010, in Tutorials, Wordpress

In this week’s tutorial we’ll show you how to display recent comments with their corresponding Gravatar. We’ll also explain how you can use a custom image, for when the commenter has no Gravatar – without a plugin. The code used in this post is very similar to the recent comment section on this site.

Look through the steps or skip straight to the code.


Step 1: Find the comments

We use the Wordpress function ‘get_comments()’ to find any comments that are avaliable and store them in an array, ready for us to process. We also throw in a few parameters to make sure we only show 5 comments and they are all approved. Any comments are stored in a variable called ‘comments’.

<?php
$comments = get_comments('status=approve&number=5');

Step 2: Were we able to retrieve any comments?

We have our results in a variable (hopefully). Before we can loop through the comments and display them, we must check they exist.

$if ($comments) { 
      // If there are comments, code to display them here.
}
else {
      // If there are no comments, this code will display.
}

Step 3: Filling in the ‘IF’ statement

Now your code has decided whether or not you have any comments it needs to display something. Below is the IF and ELSE statement, updated.

if ($comments) {
    echo '<ul id="recent_comments">';

    foreach ($comments as $comment) {
        echo '<li><a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">' . get_avatar( $comment->comment_author_email, $img_w);
        echo '<span class="recent_comment_name">' . $comment->comment_author . ': </span>';
		$comment_string = $comment->comment_content;
		$comment_excerpt = substr($comment_string,0,100);

		echo $comment_excerpt;

		if (strlen($comment_excerpt) > 99){
			echo ' ...';
		}
        echo '</a></li>';
    }
    echo '</ul>';
}
else{
	echo '<ul id="recent_comments">
	          <li>No Comments Yet</li>
	      </ul>';
}

The above goes something like this…

  1. For each comment, create a new list item, make the information inside the list a link. The link will contain the location and the title information of the comment.
  2. Display the Gravatar that matches the submitted email address.
  3. Create a span, wrap the comment author text in the span (we use this to make the author a different colour).
  4. In this case, we don’t want all of the comment text to be displayed, especially if it is a long comment. To fix this problem, we store the comment text in to a variable named $comment_string’ then we process that variable and a string 100 characters long is stored in the variable $comment_excerpt’
  5. ‘Echo’ the newly created excerpt.
  6. If the excerpt is longer than 99 characters, this indicates the whole comment could not be shown and we show that by adding ‘…’ to the end of the excerpt.
  7. Close open elements.
  8. If there are no comments make a simple unordered list which displays a text indicating no comments.

Step 4: Adding a custom default avatar.

Custom Gravatar

If the commenter does not have a Gravatar, the code will fall back on the default Wordpress avatar – a bit boring. Add the code below to your functions.php file and this will insert your custom avatar into Wordpress Settings -> Discussion, allowing you to select it.

<?php
add_filter( 'avatar_defaults', 'newgravatar' );  

function newgravatar ($avatar_defaults) {
     $myavatar = get_bloginfo('template_directory') . '/images/YOUR_IMAGE_HERE.png';
     $avatar_defaults[$myavatar] = "Custom Gravatar";
     return $avatar_defaults;
}
?>

Final comments and code

We use this block of code inside sidebar.php to display 5 most recent comments with their corresponding Gravatars, feel free to move it to another template file.

<?php
$comments = get_comments('status=approve&number=5');

if ($comments) {
    echo '<ul id="recent_comments">';

    foreach ($comments as $comment) {
        echo '<li><a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">' . get_avatar( $comment->comment_author_email, $img_w);
        echo '<span class="recent_comment_name">' . $comment->comment_author . ': </span>';
		$comment_string = $comment->comment_content;
		$comment_excerpt = substr($comment_string,0,100);

		echo $comment_excerpt;

		if (strlen($comment_excerpt) > 99){
			echo ' ...';
		}
        echo '</a></li>';
    }
    echo '</ul>';
}
else{
	echo '<ul id="recent_comments">
	          <li>No Comments Yet</li>
	      </ul>';
}
?>

The block below displays a custom Gravatar within your Wordpress settings, it must sit in your themes functions.php file.

<?php
add_filter( 'avatar_defaults', 'newgravatar' );  

function newgravatar ($avatar_defaults) {
     $myavatar = get_bloginfo('template_directory') . '/images/YOUR_IMAGE_HERE.png';
     $avatar_defaults[$myavatar] = "Custom Gravatar";
     return $avatar_defaults;
}
?>

Don’t forget to get the code working before you start chopping it to pieces :). If you have any questions please reply below.

About Fuzeo

Fuzeo content is designed, created and published by Henry Brown and Daniel Sensecall. Henry studies Web Design & Development while Dan studies Design for Digital Media. You should follow us on Twitter here.

12 Comments

Trackbacks & pingbacks

Leave your reply