have_comments()判断当前文章/页面是否有评论

目录

描述

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

have_comments() 描述

用法

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

have_comments() 用法

参数

n/a

have_comments() 参数

返回值

(bool) 

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

have_comments() 返回值

示例

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?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() ?>

have_comments() 示例

注意

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

have_comments() 注意

历史

添加于 版本: 2.2.0

have_comments() 历史

源文件

have_comments() 函数的代码位于 wp-includes/query.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Whether there are comments to loop over.
 *
 * @since 2.2.0
 *
 * @global WP_Query $wp_query
 *
 * @return bool
 */
function have_comments() {
	global $wp_query;
	return $wp_query->have_comments();
}

have_comments() 源文件

相关