1

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

1 Answer 1

0

You could do this much simpler like this:

<?php $comments = get_comments('status=approve&number=5'); ?>
<ul>
    <?php foreach ($comments as $comment) { ?>
        <li>
            <?php echo get_avatar( $comment, '35' ); ?>
            <a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="on <?php echo $comment->post_title; ?>"> <?php echo strip_tags($comment->comment_author); ?>: <?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>... </a>
        </li>
    <?php }  ?>
</ul>

Ok, here’s some code explanations:

  • $comments = get_comments('status=approve&number=5') -> get 5 latest comments
  • get_avatar( $comment, '35' ) -> get and display avatar with 35x35px size
  • get_permalink($comment->ID) and $comment->comment_ID -> get the link to the comment
  • strip_tags($comment->comment_author) -> display comment author
  • wp_html_excerpt( $comment->comment_content, 35 ) -> display the comments up to 35 characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.