Back WordPress

WordPress: Comment Count Shortcode

WRITTEN BY ON 14 Feb 2012
12,804 VIEWS • SHARES
1 comment

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

PHP

Usage: [comments id="1"]

<?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');
?>
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

1 comment
toptan karanfil 11 years ago
Thank you very useful information
Reply