Codex

Template Tags/get comments number

Contents

Description

Retrieves the value of the total number of comments, Trackbacks, and Pingbacks for a post. This tag must be within The Loop. Unlike comments_number() this function will return the value as a numeric value.

Usage

<?php $my_var = get_comments_number( $post_id ); ?>

Parameters

post_id
(integer/object) (optional) post ID.
Default: current post ID.

Examples

To make get_comments_number work like comments_number you can use this code.

$num_comments = get_comments_number(); // get_comments_number returns only a numeric value

if ( comments_open() ) {
	if ( $num_comments == 0 ) {
		$comments = __('No Comments');
	} elseif ( $num_comments > 1 ) {
		$comments = $num_comments . __(' Comments');
	} else {
		$comments = __('1 Comment');
	}
	$write_comments = '<a href="' . get_comments_link() .'">'. $comments.'</a>';
} else {
	$write_comments =  __('Comments are off for this post.');
}

Related

cancel_comment_reply_link(), comment_author(), comment_author_email(), comment_author_email_link(), comment_author_IP(), comment_author_link(), comment_author_rss(), comment_author_url(), comment_author_url_link(), comment_class(), comment_date(), comment_excerpt(), comment_form_title(), comment_form(), comment_ID(), comment_id_fields(), comment_reply_link(), comment_text(), comment_text_rss(), comment_time(), comment_type(), comments_link, comments_number(), comments_open(), comments_popup_link(), comments_popup_script(), comments_rss_link(), get_avatar(), next_comments_link(), paginate_comments_links(), permalink_comments_rss(), previous_comments_link(), wp_list_comments()

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