Comment Count Shortcode

Use this shortcode to display a specific post’s comment count. All you have to do is add the post ID.

Here is an example:

[comments id="1"]

This will display the comment count for the post with the ID of 1.

Code Snippet:

<?php
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');
?>

Posted in Posts