2012-09-27 48 views
0

我有一個小部件,檢索並顯示WordPress網站的最新評論。它顯示評論作者,Gravatar,評論和日期/時間。如何停止WordPress評論小部件與主題評論衝突

顯示註釋的功能是在一個類中。

我遇到的問題是,無論何時顯示此小部件,它都會混亂或與我爲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"; 
     } 


    } 

這是我如何調用該函數的功能:

<?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'); ?> 

起初我還以爲是有人在的Gravatar導致衝突,但是我刪除,並它沒有改變。

任何幫助將不勝感激。 謝謝

編輯: 這似乎是造成問題的get_comment_link()

如果我刪除該函數的兩個實例,則調用該小部件和註釋顯示正常。 我試過了:wp_reset_postdata();wp_reset_query();rewind_posts();都沒有效果。

回答

0

嘗試在foreach循環後添加對wp_reset_query()的呼叫。

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

+0

我曾嘗試wp_reset_query但它並沒有解決這個問題。 – Jason

+0

也許你想'rewind_posts()'然後呢? http://codex.wordpress.org/Function_Reference/rewind_posts雖然顯示8條評論中的6條是沒有意義的,但也許你的意思是6至8條? – doublesharp

+0

我知道這聽起來很奇怪,但那是怎麼回事。看看:http://www.wpinsite.com/articles/advanced-social-widget-wordpress-plugin-giveaway。現在顯示8條評論中的7條。 – Jason