I have a widget that retrieves and displays the latest comments for a WordPress site. It displays the comment author, Gravatar, comment and date / time.
The function to display the comments is in a class.
The issue that I am having is that whenever i display this widget it messes up or conflicts with the number of comments that are returned for my WordPress theme.
Example. In the widget choose to display 5 comments. On a page on the site I have a post that has 8 comments. When the widget is enabled only 6 of those 8 comments are displayed.
If I disable the widget, all the comments display.
This is the function to display the comments
/**
* Retrieves the latest comments
*
* Shows a list of latest comments ordered by the date added
*
* @param int $limit - The number of posts to display
* @param int $chars - The number of characters to display for the post body
* @param int $size - Size of the comment Gravatar
* @param boolean $displayCommentsIcon - Whether to display the comment Gravatar
*
*/
public function aaw_get_latest_comments($display_comments_icon = true, $comments_icon_size = 50, $comments_amount = 5, $comments_chars = 35, $display_comments_date = true) {
global $comments;
$com_excerpt = '';
$aaw_comments = get_comments(array('number' => $comments_amount, 'status' => 'approve'));
if($aaw_comments){
foreach((array)$aaw_comments as $aaw_comment){
if($comments_chars > 0) {
$com_excerpt = self::aaw_snippet_text($aaw_comment->comment_content, $comments_chars);
}
echo '<li>';
if($display_comments_icon == 'true'){
echo '<a href="'.esc_url(get_comment_link($aaw_comment->comment_ID) ).'" title="'. __('Commented on: ', $this->hook). $aaw_comment->post_title.'">';
echo get_avatar($aaw_comment, $comments_icon_size);
echo '</a>';
}
echo '<div class="aaw_info">';
echo '<a href="'.esc_url(get_comment_link($aaw_comment->comment_ID) ).'" title="'. __('Commented on: ', $this->hook). $aaw_comment->post_title.'">';
echo '<i>'.strip_tags($aaw_comment->comment_author).'</i>: '.strip_tags($com_excerpt).'...';
echo '</a>';
if($display_comments_date == 'true'){
echo '<span class="aaw_meta">'.get_comment_date('j M Y',$aaw_comment->comment_ID).' '.__('at', $this->hook).' '.get_comment_date('g:i a',$aaw_comment->comment_ID).'</span>';
}
echo '</div>';
echo '</li>';
}
} else {
echo '<li>'.__('No comments available', $this->hook).'</li>'."n";
}
}
This is how I call the function:
<?php $advanced_activity_widget->aaw_get_latest_comments($display_comments_icon == 'true' ? 'true' : 'false', $comments_icon_size, $comments_amount, $comments_chars, $display_comments_date == 'true' ? 'true' : 'false'); ?>
At first I thought is was the Gravatar causing the conflict however I removed it and it didn’t make a change.
Any help would be greatly appreciated.
Thanks
You could do this much simpler like this:
Ok, hereâs some code explanations:
$comments = get_comments('status=approve&number=5')
-> get 5 latest commentsget_avatar( $comment, '35' )
-> get and display avatar with 35x35px sizeget_permalink($comment->ID)
and$comment->comment_ID
-> get the link to the commentstrip_tags($comment->comment_author)
-> display comment authorwp_html_excerpt( $comment->comment_content, 35 )
-> display the comments up to 35 characters