WordPress获取最新评论WP_Comment_Query()方法

一、通过WP_Comment_Query()方法获取

function bg_recent_comments($no_comments = 5, $comment_len = 80, $avatar_size = 48) {
    $comments_query = new WP_Comment_Query();
    $comments = $comments_query->query( array( 'number' => $no_comments ) );
    $comm = '';
    if ( $comments ) : foreach ( $comments as $comment ) :
        $comm .= '<li><a class="author" href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">';
        $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;
}

实例:

<div id="tab-mh_magazine_tabbed-3-3" class="mh-tab-content mh-tab-comments">
                <ul class="mh-tab-content-comments">
                    <?php
                    $comments_query = new WP_Comment_Query();
                    $comments = $comments_query->query( array( 'number' => 3 ) );

                    // The comment loop
                    if ( $comments ) : foreach ( $comments as $comment ) :
                            ?>
                            <li class="mh-tab-comment-item">
                            <span class="mh-tab-comment-avatar">
                            <?php echo get_avatar( $comment->comment_author_email, 24 ); ?>
                            </span>
                                <span class="mh-tab-comment-author">
                                <?php echo get_comment_author( $comment->comment_ID ) ?>:
                            </span>
                                <a href="<?php echo get_permalink( $comment->comment_post_ID ) ?>">
                                    <span class="mh-tab-comment-excerpt"><?php echo strip_tags( substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, 30 ) ) . '...' ?></span>
                                </a>
                            </li>

                            <?php
                    endforeach; else :
                       echo 'No comments.';
                    endif;
                    ?>
                </ul>
            </div>

二、通过自定义SQL获取

<?php //Get Recet Comments Function
function get_recent_comments($no_comments = 10, $comment_len = 35) {
   
   global $wpdb;
   
    $request  = "SELECT * FROM $wpdb->comments";
    $request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
    $request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
    $request .= " ORDER BY comment_date DESC LIMIT $no_comments";
       
    $comments = $wpdb->get_results($request);
       
    if ($comments) {
        foreach ($comments as $comment) {
            ob_start();
            ?>
                <li>
                    <a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?>:</a>
                    <?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?>
                </li>
            <?php
            ob_end_flush();
        }
    } else {
        echo '<li>'.__('No comments', 'banago').'</li>';
    }
}

function dp_get_author($comment) {
    $author = "";

    if ( empty($comment->comment_author) )
        $author = __('Anonymous', 'banago');
    else
        $author = $comment->comment_author;
       
    return $author;
}