For my WP-theme I am displaying comments for each blog-post. Everything works except my else
statement, I figured this is because every $post
counts ALL $comments
on the page, instead of just its own.
So this is my 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>";
} ?>
I tried with two different foreach statements, first with $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; ?>
Using $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; ?>
The second just rendered “This post is password protected. Enter the password to view comments.” instead of include('intranet-comments.php');
(which is called after the comments).
You should pass postID as argument to
get_comments_number
instead of array of comments. Check wp codex.And why are you trying to display comments when number of comments is 0 ?
Should look something like this: NOT TESTED. Answered via Smartphone