플러그인으로 최근 댓글 가져오는 것이 아니라 php 상으로 삽입할 때 사용하는 방법이다.
스킨 폴더안의 functions.php 를 열어 아래 코드를 추가한다.
function bg_recent_comments($no_comments = 5, $comment_len = 30, $avatar_size = 48) {
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query( array( 'number' => $no_comments ) );
$comm = '';
if ( $comments ) : foreach ( $comments as $comment ) :
$textoverflow = (strlen($comment->comment_content) > $comment_len) ? '...' : '';
$comm .= '<li><a href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">';
$comm .= strip_tags( iconv_substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, $comment_len,"utf-8" ) ) .$textoverflow. '</a></li>';
// $comm .= get_avatar( $comment->comment_author_email, $avatar_size );
// $comm .= get_comment_author( $comment->comment_ID ) . ':</a> ';
// $comm .= '<p>' . strip_tags( substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, $comment_len ) ) . '...</p></li>';
endforeach; else :
$comm .= 'No comments.';
endif;
echo $comm;
}
주석으로 체크한 부분은 아바타 정보나 등록자 정보를 가져오는 것들이니 필요에 따라 주석을 해제한 후 사용하면 된다.
이제 위 함수를 실제 최근 댓글 목록을 노출할 부분에서 호출하면 된다.
예를 들어 스킨 폴더의 single.php 나 sidebar-content.php 같은 곳이다.
<section class="recent">
<h3>comment</h3>
<ul>
<?php bg_recent_comments(); ?>
</ul>
</section>
이렇게 사용하면 ul 밑으로 목록화 되어 li들이 노출된다.