WordPress Snippets at WPcustoms

Shortcode to show comments count

Use this shortcode to show comments count for a specific post. This can also be modified into a function which can be used in your theme grabbing the $post-ID


/**
 * Snippet Name: Shortcode to show comments count
 * Snippet URL: https://wpcustoms.net/snippets/shortcode-show-comments-count/
 */
  //usage: [comments id="1"]

function comments_shortcode($atts) {
 extract( shortcode_atts( array( 'id' = ''),
 $atts ) );

	$num = 0;
	$post_id = $id;
	$queried_post = get_post($post_id);
	$cc = $queried_post->comment_count;
		if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments';
		else : $cc = $cc.' Comment';
		endif;
	$permalink = get_permalink($post_id);

	return '' . $cc . '';

}
add_shortcode('comments_count', 'comments_shortcode');