试图获得wordpress评论以显示评论内容

时间:2013-12-01 12:52:36

标签: php wordpress comments

在我的主页上。我想要一个评论列表与用户评论一起显示,附上他们的头像,名称以及评论附加到的类别。

此时我从下图中得到了这些结果

enter image description here

我似乎无法将消息的实际内容与帖子附加到的类别一起显示在循环中。

<?php
$total_comments = $wpdb->get_results("SELECT comment_date_gmt, comment_author, comment_ID, comment_post_ID, comment_author_email FROM $wpdb->comments WHERE comment_approved = '1' and comment_type != 'trackback' ORDER BY comment_date_gmt DESC LIMIT 10");
$comment_total = count($total_comments);
echo '<ul>';
for ($comments = 0; $comments < $comment_total; $comments++) {
echo "<div style='clear:both;width:355px;padding-top:3px;'><div style='float:left;width:35px;'>";
echo get_avatar($total_comments[$comments]->comment_author_email,$size='32',$default='<path_to_url>' );
echo "</div> <div style='width:320px;'>";
echo '<li>';
echo $total_comments[$comments]->comment_author . ' says ';
echo '<a href="'. get_permalink($total_comments[$comments]->comment_post_ID) . '#comment-' . $total_comments[$comments]->comment_ID . '">';
echo get_the_title($total_comments[$comments]->comment_post_ID);
echo '</a></li></div></div>';
}
echo '</ul>'
?>

我如何添加comment_content及其类别?

EDIT。以下是我发现的,它显示了一些接近我的预先答案的内容

<?php
$comments = get_comments('status=approve&number=5');
  foreach($comments as $comment) :?>

  <?php $my_id = $comment->comment_post_ID ; $post_id_comms = get_post($my_id); $title = $post_id_comms->post_title;?> 

    Who: <?php echo($comment->comment_author);?><br />
    About: <a href="<?php echo get_permalink($my_id) ?>#comment-<?php echo $comment->comment_post_ID?>" title="on <?php echo $title ?>"><?php echo $title ?></a><br />
    What they said: <?php echo($comment->comment_content);?><br />
    When they said it: <?php echo($comment->comment_date);?><br />

  <?php endforeach;?> 

1 个答案:

答案 0 :(得分:0)

尝试使用get_comments(),它会为您提供所需的大部分信息,并从评论中获取帖子的类别,您可以在循环中使用get_the_category函数,例如

$comments = $comments = get_comments( array('status') => 'approve' );
foreach($comments as $comment) {
    echo $comment->comment_content;
    $categories = get_the_category( $comment->comment_post_ID );
    foreach($categories as $category) {
        echo $category->cat_name;
    }
}