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

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

通过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 .= '

  • ';
    $comm .= get_avatar( $comment->comment_author_email, $avatar_size );
    $comm .= get_comment_author( $comment->comment_ID ) . ':
    ';
    $comm .= '

    ' . strip_tags( substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, $comment_len ) ) . '...

  • ';
    endforeach; else :
    $comm .= 'No comments.';
    endif;
    echo $comm;
    }

    Recent Comments

    .recent-comments { list-style: none; font-size: 12px; color: #485358; }
    .recent-comments li { overflow: hidden; padding: 20px 0; border-top: 1px dotted #DADEE1; }
    .recent-comments li:first-child { border: 0 none; }
    .recent-comments img { float: left; margin-right: 8px; }
    .recent-comments a { display: block; margin-top: 10px; padding-top: 10px; text-transform: uppercase; }

    通过自定义SQL获取


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

  • :
    comment_content), 0, $comment_len)); ?>
  • '.__('No comments', 'banago').'

    ';
    }
    }

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

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

    return $author;
    }

    Leave a Comment