Wordpress Else 语句在 $comments 循环中不起作用

Wordpress Else statement not working in $comments loop

对于我的 WP 主题,我显示每个博客的评论-post。除了我的 else 语句外,一切正常,我认为这是因为每个 $post 都计算页面上的所有 $comments,而不仅仅是它自己的。

所以这是我的 php:

<?php 
    $args = array(
        'post_id' => $post->ID,
        'status' => 'approve'
    );
    $comments = get_comments($args); ?>

        if (get_comments_number($comments)==0) {
            wp_list_comments(array('per_page' => 10, // Allow comment pagination
            'reverse_top_level' => false //Show the latest comments at the top of the list ), $comments); 
            } else {
                echo "<i>No Comments</i>";
     } ?>

我尝试了两个不同的 foreach 语句,首先是 $comments:

<?php 
    foreach($comments as $comment) :
        if (get_comments_number($comments)==0) {
            wp_list_comments(array('per_page' => 10, // Allow comment pagination
            'reverse_top_level' => false //Show the latest comments at the top of the list ), $comments); 
            } else {
                echo "<i>Inga kommentarer än</i>";
     } ?>
  } endforeach; ?>

使用 $posts:

<?php 
        foreach($posts as $post) :
            if (get_comments_number($comments)==0) {
                wp_list_comments(array('per_page' => 10, // Allow comment pagination
                'reverse_top_level' => false //Show the latest comments at the top of the list ), $comments); 
                } else {
                    echo "<i>Inga kommentarer än</i>";
         } ?>
      } endforeach; ?>

第二个只是呈现 "This post is password protected. Enter the password to view comments." 而不是 include('intranet-comments.php');(在注释之后调用)。

您应该将 postID 作为参数传递给 get_comments_number 而不是评论数组。检查 wp codex。 你为什么要在评论数为 0 时显示评论?

应该看起来像这样:未测试。通过智能手机回答

<?php 
$args = array(
    'post_id' => $post->ID,
    'status' => 'approve'
);
    if (get_comments_number($post->ID)!=0) {
          $comments = get_comments($args);
          // and do something with commenzs
        } else {
            echo "<i>No Comments</i>";
 } ?>