• Could someone help me add a Gravatar to the following recent comments code? Thank you.

    <h2>Recent Comments</h2>
    <?php
    global $wpdb;
    $sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,50) AS com_excerpt
    FROM $wpdb->comments
    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    WHERE comment_approved = ‘1’ AND comment_type = ” AND
    post_password = ”
    ORDER BY comment_date_gmt DESC
    LIMIT 5″;
    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML;
    $output .= “\n

    “;
    $output .= $post_HTML;
    echo $output;?>
    </div>

Viewing 5 replies - 1 through 5 (of 5 total)
  • I suggest you to use the RecentComments Plugin. It comes with gravatar and widget support and tons of other features.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Two steps to modify that code:

    1. Add comment_author_email to the list of things it’s getting in the SELECT statement.

    2. echo get_avatar( $comment->comment_author_email, 32 ); where you want the gravatar in the comments loop.

    A while back I edited a version needing updated badly. I don’t remember were I got it but this plugin seems to work fine in 2.8

    The Plugin:

    <?php
    /*
    Plugin Name: Recent Comments with Gravatar:
    Version: 0.2
    Description: Recent Comments with Gravatar. Editied for WordPress  2.8.0
    Author: Unknown
    */
    
    function recent_comments($g_size = 30, $no_comments = 10, $comment_lenth = 60, $show_pass_post = false) {
            global $wpdb, $tablecomments, $tableposts;
    
            $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_email FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND post_status = 'publish' ";
    
            if(!$show_pass_post) { $request .= "AND post_password ='' "; }
    
            $request .= "AND comment_approved = '1' ORDER BY $tablecomments.comment_date DESC LIMIT $no_comments";
            $comments = $wpdb->get_results($request);
    
            foreach ($comments as $comment) {
    		$comment_id = $comment->comment_ID;
    		$comment_content = strip_tags($comment->comment_content);
    		$comment_excerpt = mb_substr($comment_content, 0, $comment_lenth)." [...]";
    		$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
    		$comment_author_email = $comment->comment_author_email;
    
       echo '<div class="gravitar">';
       echo "<p class=\"li".$comment_id."\">";
    		if (function_exists('get_avatar')) {
    					  if ('' == $comment->comment_type) {
    						 echo get_avatar($comment->comment_author_email, 40);
    					  } elseif ( ('trackback' == $comment->comment_type) || ('pingback' == $comment->comment_type) ) {
    						 echo get_avatar($comment->comment_author_url, 40);
    					  }
    				   } elseif (function_exists('gravatar')) {
    					  echo "<img src=\"";
    					  if ('' == $comment->comment_type) {
    						 echo gravatar($comment->comment_author_email);
    					  } elseif ( ('trackback' == $comment->comment_type) || ('pingback' == $comment->comment_type) ) {
    						 echo gravatar($comment->comment_author_url);
    					  }
    					  echo "\" alt=\"\" class=\"avatar\" />";
    				   }
    
        echo "</p>\n";
        echo '</div>' ;
    
                echo '<div class="comments_exrpt">';
    			echo " <a href=\"" . $permalink . "\" title=\"View the entire comment\">";
    			echo $comment_excerpt;
    			echo "</a>";
    			echo '</div>' ;
    
    	            }
    
    }
    ?>

    Add this to your theme:

    <?php
      if(function_exists('recent_comments')) {
      	recent_comments(30, 10, 100, false);
      }
    ?>

    css:

    .comments_exrpt{margin-bottom:15px;width:260px;}
    .comments_exrpt a{color:#ffc;font-family:Verdana;font-size:65.5%;}

    great plugin, mobster, workd great but im trying to push it further

    im trying to order or group the comments by posts, and displaying the post title above the comments

    something like this

    Post title
    Gravatar – Author @ date, time
    Comment excerpt

    Gravatar – Author @ date, time
    Comment excerpt
    *****
    Post Title 2
    Gravatar – Author @ date, time
    Comment excerpt

    Gravatar – Author @ date, time
    Comment excerpt

    all i have done is to retrieve the post author and date, but the post title doesn’t work, it echoes the current post title

    any help will be very appreciated

    I actually have a function that you can just call from your recent comments loop.

    function gravatar($rating = false, $size = false, $default = false, $border = false) {
    	global $comment;
    	$out = "https://www.gravatar.com/avatar/".md5($comment->comment_author_email)."png";
    	if($rating && $rating != '')
    		$out .= "?r=".$rating;
    	if($size && $size != '')
    		$out .="&s=".$size;
    	if($default && $default != '')
    		$out .= "&d=".urlencode($default);
    	if($border && $border != '')
    		$out .= "&border=".$border;
    	echo $out;
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Gravatar to Recent Comments’ is closed to new replies.