如何阻止WordPress评论小部件与主题评论冲突

如何阻止WordPress评论小部件与主题评论冲突,wordpress,widget,comments,conflict,Wordpress,Widget,Comments,Conflict,我有一个小部件,用于检索和显示WordPress站点的最新评论。它显示注释作者、Gravatar、注释和日期/时间 显示注释的函数位于类中 我遇到的问题是,每当我显示这个小部件时,它都会弄乱或与我的WordPress主题返回的评论数量冲突 例如。在小部件中,选择显示5条注释。在网站的一个页面上,我有一篇有8条评论的帖子。当小部件启用时,仅显示这8条注释中的6条 如果禁用小部件,将显示所有注释 这是显示注释的功能 /** * Retrieves the latest comments

我有一个小部件,用于检索和显示WordPress站点的最新评论。它显示注释作者、Gravatar、注释和日期/时间

显示注释的函数位于类中

我遇到的问题是,每当我显示这个小部件时,它都会弄乱或与我的WordPress主题返回的评论数量冲突

例如。在小部件中,选择显示5条注释。在网站的一个页面上,我有一篇有8条评论的帖子。当小部件启用时,仅显示这8条注释中的6条

如果禁用小部件,将显示所有注释

这是显示注释的功能

/**
     * 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";
        }


    }
/**
*检索最新的注释
*
*显示按添加日期排序的最新评论列表
*
*@param int$limit-要显示的帖子数
*@param int$chars-要为帖子正文显示的字符数
*@param int$size-注释Gravatar的大小
*@param boolean$displayCommentsIcon-是否显示注释Gravatar
*
*/
公共函数aaw_获取最新注释($display_comments_icon=true,$comments_icon_size=50,$comments_amount=5,$comments_chars=35,$display_comments_date=true){
全球$评论;
$com_摘录=“”;
$aaw_comments=get_comments(数组('number'=>$comments\u amount,'status'=>approve');
如果($aaw_评论){
foreach((数组)$aaw_注释作为$aaw_注释){
如果($comments\u chars>0){
$com\u extract=self::aaw\u snippet\u text($aaw\u comment->comment\u content,$comments\u chars);
}
回音“
  • ”; 如果($display\u comments\u icon=='true'){ 回声'; } 回声'; 回声'; 如果($display\u comments\u date=='true'){ 回显''获取注释日期('j M Y',$aaw注释->注释ID)。''。''.'获取注释日期('g:i a',$aaw注释->注释ID)。''; } 回声'; 回音“
  • ”; } }否则{ 回显“
  • ”。“('No comments available',$this->hook)。”
  • 。“\n”; } }
    这就是我调用函数的方式:

    <?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'); ?>
    
    
    
    起初我以为是坟墓造成了冲突,但是我把它移走了,它没有改变

    任何帮助都将不胜感激。 谢谢

    编辑: 似乎是
    get\u comment\u link()
    导致了这些问题

    如果我删除该函数的两个实例,则调用小部件和注释显示良好。
    我试过:
    wp_reset_postdata()
    wp_reset_query()
    倒带帖子()全部无效。

    尝试在
    foreach
    循环之后添加对
    wp\u reset\u query()
    的调用


    我确实尝试了wp\u reset\u查询,但它没有解决问题。也许你想要
    倒带帖子()
    那么?虽然8条评论中有6条是没有意义的,但也许你的意思是6到8条?我知道这听起来很奇怪,但事情就是这样发生的。看一看:。现在显示了8条注释中的7条。我猜这是您的示例代码中未包含的内容。。。我看到一些东西没有包括在内,比如从未使用过的
    global$comments
    ,以及未定义的
    $this->hook
    。是的,对不起,$this->hook我已经删除了。我使用了一个函数的这段代码。我在类中定义了$this->hook。根据我的调查,这似乎与这个调用有关:esc\u url(get\u comment\u link($comment->comment\u ID))