Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress Else语句在$comments循环中不工作_Php_Wordpress_If Statement_Comments - Fatal编程技术网

Php Wordpress Else语句在$comments循环中不工作

Php Wordpress Else语句在$comments循环中不工作,php,wordpress,if-statement,comments,Php,Wordpress,If Statement,Comments,对于我的WP主题,我会为每一篇博客文章显示评论。除了我的else语句之外,其他一切都正常工作,我想这是因为每个$post都计算页面上的所有$comments,而不仅仅是它自己的 这是我的php: <?php $args = array( 'post_id' => $post->ID, 'status' => 'approve' ); $comments = get_comments($args); ?>

对于我的WP主题,我会为每一篇博客文章显示评论。除了我的
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>";
     } ?>

如果(获取注释数量($comments)==0){
wp_list_注释(数组('per_page'=>10,//允许注释分页
“reverse_top_level”=>false//在列表顶部显示最新评论),$comments);
}否则{
回应“无评论”;
} ?>
我尝试了两种不同的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; ?>

}endforeach;?>
使用$POST:

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

}endforeach;?>

第二个是“这篇文章有密码保护。输入密码查看评论。”而不是
include('intranet-comments.php')(在注释后调用)

您应该将postID作为参数传递给
get\u comments\u number
,而不是注释数组。检查。 当评论数为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>";
 } ?>