问题:Wordpress - 为什么 have_comments() 不起作用?

在过去的几个小时里,我一直在解决这个问题,但找不到原因,也找不到任何在线帮助。

差不多,我正在从头开始制作自定义主题。 WordPress 总是以默认的“Hello world!”开头。帖子以及与该帖子关联的默认评论。

“世界你好!” post 不应评估为 false,但由于某种原因它确实如此。如您所见,我尝试使用 WordPress 提供的 comments_templates 函数来允许从名为“comments.php”的文件中运行代码,但这只是为我的问题添加了一个错误因素,并且这样做,“评论”中的代码.php" 甚至从未被访问过,所以现在,我将其排除在外。


执行以下代码时显示的内容


证明有一个应该显示的评论


<?php
  if(have_posts()) : while(have_posts()) : the_post();
    get_template_part('content', get_post_format());
    // comments_template('/comments.php', true);
    if(have_comments()) :
      echo 'OMG COMMENTS WORK';
      foreach (get_comments() as $comment) :
        echo $comment -> comment_content;
        echo $comment -> comment_author;
        echo apply_filters('comment_text', $comment -> comment_content);
      endforeach;
    else :
      echo 'There are no comments to display here (this statement may or may not be true...)';
    endif;
  endwhile; endif;
?>

解答

阅读Codex:警告:在调用 comments_template 之前,此函数将始终返回“false”。如果您需要在调用 comments_template 之前检查评论,请改用 get_comments_number。

Logo

更多推荐