WordPress.org

Ready to get started?Download WordPress

Codex

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

Template Tags/get comments number

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

Comments Functions

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