WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/have comments

Description

Determines whether there are any Comments to loop over or not.

Usage

This function relies upon the global $wp_query object to be set - this is usually the case from within The Loop.

Warning: this function will always return "false" until after comments_template has been called. If you need to check for comments before calling comments_template, use get_comments_number instead.

<?php $have_comments have_comments(); ?>

Parameters

n/a

Return Values

(bool) 
true if the current query has comments available, false otherwise.

Examples

Example based on Twentyten's comments.php template: Comments title (and more) is displayed only when comments are available:

<?php if ( have_comments() ) : ?>
	<h3 id="comments-title"><?php
		printf(
			_n( 'One Response to %2$s', '%1$s Responses to %2$s', get_comments_number(), 'twentyten' ),
			number_format_i18n( get_comments_number() ),
			'<em>' . get_the_title() . '</em>' 
		);
	?></h3>
// [and more, of course...]
<?php else : // or, if we don't have comments:
	if ( ! comments_open() ) : ?>
		<p class="nocomments"><?php _e( 'Comments are closed.', 'twentyten' ); ?></p>
	<?php endif; // end ! comments_open() ?>
<?php endif; // end have_comments() ?>

Notes

  • Uses global: (object) $wp_query to determine if comments are available (via have_comments method).

Change Log

Since: 2.2.0

Source File

have_comments() is located in wp-includes/query.php.

Related

  • the_comment
See also index of Function Reference and index of Template Tags.