0
votes

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

EDIT: It seems to be the get_comment_link() that is causing the problems.

If I remove both instances of that function call the widget and comments display fine. I have tried: wp_reset_postdata(); wp_reset_query(); rewind_posts(); all to no effect.

1

1 Answers

0
votes

Try adding a call to wp_reset_query() after your foreach loop.

http://codex.wordpress.org/Function_Reference/wp_reset_query