Using WordPress ‘have_comments()’ PHP function

The have_comments() WordPress PHP function determines whether the current WordPress query has comments to loop over.

Usage

if (have_comments()) {
    // Your code here
}

Parameters

  • None

More information

See WordPress Developer Resources: have_comments()

Examples

Display comments title and count

if (have_comments()) {
    $comments_title = sprintf(
        _n('One Response to %2$s', '%1$s Responses to %2$s', get_comments_number(), 'your_theme'),
        number_format_i18n(get_comments_number()),
        get_the_title()
    );
    echo "<h3 id=\"comments-title\">{$comments_title}</h3>";
}

Display comments list

if (have_comments()) {
    wp_list_comments();
}

Display a message when there are no comments

if (have_comments()) {
    wp_list_comments();
} else {
    echo "<p>No comments yet, be the first to comment!</p>";
}

Display pagination for comments

if (have_comments()) {
    wp_list_comments();
    paginate_comments_links();
}

Display a message when comments are closed

if (have_comments()) {
    wp_list_comments();
} elseif (!comments_open()) {
    echo "<p>Comments are closed.</p>";
}