WordPress have_comments does not work

I have set a comment() condition in wordpress. This condition is set in comments.php just as the wordpress default theme does.

Then the whole comments.php file is loaded using comment_template; Now, when I remove have_comments() condition, everything works and all the comments are loaded, but when I add this condition, it returns false as if there are no comments.

Read More

Here is my whole comments.php file:

<?php
/**
| This page deals with the comment-system and template in the Behdis Marketing Group WordPress Theme.   
**/
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array(
    'author' => "<div><input type='text' name='author' placeholder='Full Name' /></div>",
    'email'  => "<div><input type='text' name='email' placeholder='Email /></div>", 
);

$comments_args = array(
    'fields' =>  $fields,
    'comment_field' => "<div class="comment-component"><textarea name="comment" id="comment" ></textarea></div>",
    'comment_notes_after' => '',
    'title_reply' => 'Write your comment...',
    'title_reply_to' => 'Reply',
    'label_submit' => 'Comment!',
    'comment_notes_before' => "<p class='simple-title'>" . 'Your email is kept secret forever' . ' '
);

comment_form($comments_args);
?>
<?php

    if( have_comments() )
    {       
?>
<section class='post-comments'>

    <?php
        $comments = get_comments();
        foreach($comments as $comm)
        {
            ?>
            <div class='post-each-comment'>
            <p class="post-each-comment-meta">
            <?php echo $comm->comment_author;?> در تاریخ <?php comment_time();?>
            </p>
            <?php echo $comm->comment_content;   ?>
            </div>
            <?php
        }
        ?>
</section>
    <?php
    }// end of have_comments()
   else
    {
        ?>
        <div class='no-comment' >
            No comments, be the first visitor to comment on this post!
        </div>
        <?php   
    }
    ?>

thanks in advance

Related posts

Leave a Reply

3 comments

  1. You call have_comments() before you call get_comments().

    It’s likely just the issue you have the wrong processing-flow here. WordPress makes use of global static state so the order of things is important (and easy to miss):

    <?php
    
        $comments = get_comments();
    
        if( have_comments() )
        {       
    ?>
    <section class='post-comments'>
    
        <?php        
            foreach($comments as $comm)
            {
                ?>
                <div class='post-each-comment'>
    

    Also codex says that have_comments() depends on the loop so that is $post. It could be that even my example code suggestion above does not make it to deal with the static state properly so you need to trouble-shoot this is a little to find out what to use.

    E.g. as get_comments() returns an array this normally does it:

    <?php
    
        $comments = get_comments();
    
        if( $comments )
        {       
    ?>
    <section class='post-comments'>
    
        <?php        
            foreach($comments as $comm)
            {
                ?>
                <div class='post-each-comment'>
    

    As you can see a call to have_comments() is not necessary.

    Hope this helps and take care.

  2. This problem occurs when you query for comments before setting up the comments. You can use comments_template(..) to load the page containing have_comments() to set up the comments properly.

  3. I spent some hours for it. Might be it will be helpful for somebody.

    When I tried to use get_template_part('comments'); in my template for using comments.php on page – it didn’t work and have_comments(); returned me bool(false)

    Thanks for this post. I’ve used comments_template(); instead get_template_part. And it works. I think that main cause is WordPress makes use of global static state, and you need to use comments_template() with single post or page to save it.