(主持人注意: 原始标题是“有没有办法使用WP-Query()?

你好!我正在运行自定义循环 使用wp_query, ,仅在特定类别中显示一个帖子 home.php 页面,像这样:

<?php $pr_q = "cat=11&posts_per_page=1"; $pregunta_q = new WP_Query($pr_q); ?>
<?php while ($pregunta_q->have_posts()) : $pregunta_q->the_post(); ?>
    <!-- post stuff here -->
<?php endwhile; ?>

有什么方法可以显示该特定帖子的评论?我尝试在循环中包括评论模板,什么也没有。是否有一个函数可以加载我可以在home.php或其他任何地方使用的特定帖子的注释?

有帮助吗?

解决方案

要能够在循环中使用注释模板,请添加

global $withcomments; $withcomments = true;

其他提示

默认情况下,如果您是

  1. 查看评论提要,或
  2. 看着 singular 物品。

您的查询不会自动提取评论,因为作为类别列表(即使只有一个),它不算为“单数”。幸运的是,有一种解决方法。基本上,在删除注释模板之前,您应该获取评论并将其放入您使用的查询对象:

<?php $pr_q = "cat=11&posts_per_page=1"; $pregunta_q = new WP_Query($pr_q); ?>
<?php while ($pregunta_q->have_posts()) : $pregunta_q->the_post(); ?>
  <!-- post stuff before comments here -->
  $comments = get_comments( array(
    'post_id' => $post->ID,
    'orderby' => 'comment_date_gmt',
    'status' => 'approve',
  ) );
  if(!empty($comments)){
    $pregunta_q->comments = $comments;
    $pregunta_q->comment_count = count($comments);
  }
  <!-- comment stuff here -->
<?php endwhile; ?>

John P Bloch答案是正确的。仅当您使用单数和评论供稿时,评论才会加载。幸运的是,我们可以通过在WP查询上添加其他参数来覆盖此行为。

'withcomments' => 1, 'feed' => 1

无需WP_QUERY或自定义循环以进行评论。您只需在发布自定义循环或发布WP_QUERY中获取发布评论即可。在循环中,您有post_id。使用post_id获取评论。代码在下面编写。

$comments = get_comments('post_id='.$post->ID);
       foreach($comments as $comment) :
               print_r($comment);
       endforeach;
许可以下: CC-BY-SA归因
scroll top