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";
}
}
これが私が関数を呼び出す方法です:
<?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();
すべて効果がありません。