wordpress snippets last updated   : October 6, 2014
we now have 641 snippets

Display the comment count using a shortcode

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will let you display the comment count for any post by simply using this shortcode [comments id=”23″ ] replacing the ID with the post ID you want to display the count for.

snippet :  PHPcopy
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 '<a href="'. $permalink . '" class="comments_link">' . $cc . '</a>';

}
add_shortcode('comments', 'comments_shortcode');
snippet :  SHORTCODEcopy
[comments id="23" ]

Did you like this post? If so please share it with your friends. Thanks!